********************************************************************************
* Paper: Sore Losers on Election Night? Examining the Temporal Emergence of the Winner–Loser Gap

* Author: Einar Bäckström
* Last Updated: 2025.04.15
* Notes:      
* - Adjust directory paths on lines 22 and 25 to match the location of the raw data.
* - Run the entire do-file to reproduce all results.
* - Output will be automatically saved in the folders: Data, Tables, and Figures.
********************************************************************************

* Set Stata version and clear memory
version 16.1
clear all
set more off

********************************************************************************
* Define directory paths
********************************************************************************

// Set main working directory to where the raw data is stored 
cd "SET MY OWN DIRECTORY"

// Set directory paths
global dir "SET MY OWN DIRECTORY"
global Data "$dir/Data"
global Tables "$dir/Tables"
global Figures "$dir/Figures"

// Create a 'Data' folder if it doesn't already exist
if !fileexists("$Data") {
    mkdir "$Data"
}

// Create the 'Figures' folder if it doesn't already exist
if !fileexists("$Figures") {
    mkdir "$Figures"
}

// Create the 'Tables' folder if it doesn't already exist
if !fileexists("$Tables") {
    mkdir "$Tables"
}

********************************************************************************
* Ensure rdrobust is installed (install from official source if missing)
********************************************************************************

capture which rdrobust
if _rc {
    display "Installing rdrobust..."
    net install rdrobust, from(https://raw.githubusercontent.com/rdpackages/rdrobust/master/stata) replace

}

********************************************************************************
* Spain 2019 
********************************************************************************

// Upload dataset
use "ESS9, Spain.dta", clear

// -----------------------------------------------------------------------------
// Date Variables: Calculate the Cut-off Dates and Respondent Interview Dates
// -----------------------------------------------------------------------------

// Define the election day reference date in Stata's date format
gen ref_date_election = mdy(11, 11, 2019)

// Calculate the observation date in Stata's date format using "inwyys", "inwmms", and "inwdds"
gen obs_date = mdy(inwmms, inwdds, inwyys)

// Calculate the difference in days between the observation date and the reference date
gen days_election = obs_date - ref_date_election

// Create a dummy variable for whether the interview occurred before or after election day
gen after_election = 0
replace after_election = 1 if days_election>0

// Generate the reference date for the establishment of the election outcome in Stata's date format
gen ref_date = mdy(1, 8, 2020)

// Calculate days between interview date and the date when election results became clear 
gen days = obs_date - ref_date

// Create a dummy variable for observations made before or after the election results solidified
gen after = 0
replace after = 1 if days>0

// -----------------------------------------------------------------------------
// Manual classification of winners and losers
// -----------------------------------------------------------------------------

// Create dummy for electoral winners (1 = supported a party that entered government following the election, 0 = supported a party that remained in or moved to opposition)
gen winner =.

// Assign winner status based on vote in last election (prtvtees)
replace winner = 1 if prtvtees == 2 
replace winner = 1 if prtvtees == 3 
replace winner = 1 if prtvtees == 7 
replace winner = 1 if prtvtees == 6 

replace winner = 0 if prtvtees == 1 
replace winner = 0 if prtvtees == 5 
replace winner = 0 if prtvtees == 17 
replace winner = 0 if prtvtees == 16 
replace winner = 0 if prtvtees == 8 
replace winner = 0 if prtvtees == 9 
replace winner = 0 if prtvtees == 10 
replace winner = 0 if prtvtees == 11 
replace winner = 0 if prtvtees == 12 
replace winner = 0 if prtvtees == 13 
replace winner = 0 if prtvtees == 14 
replace winner = 0 if prtvtees == 15 
replace winner = 0 if prtvtees == 18 
replace winner = 0 if prtvtees == 19 
replace winner = 0 if prtvtees == 20 
replace winner = 0 if prtvtees == 53 

// If available, override with winner status based on party respondent currently feels closest to (prtclfes)
replace winner = 1 if prtclfes == 2 
replace winner = 1 if prtclfes == 3 
replace winner = 1 if prtclfes == 7 
replace winner = 1 if prtclfes == 6 

replace winner = 0 if prtclfes == 1 
replace winner = 0 if prtclfes == 5 
replace winner = 0 if prtclfes == 17 
replace winner = 0 if prtclfes == 16 
replace winner = 0 if prtclfes == 8 
replace winner = 0 if prtclfes == 9 
replace winner = 0 if prtclfes == 10 
replace winner = 0 if prtclfes == 11 
replace winner = 0 if prtclfes == 12 
replace winner = 0 if prtclfes == 13 
replace winner = 0 if prtclfes == 14 
replace winner = 0 if prtclfes == 15 
replace winner = 0 if prtclfes == 18 
replace winner = 0 if prtclfes == 19 
replace winner = 0 if prtclfes == 20 
replace winner = 0 if prtclfes == 53 

// Assign winner status at time of the interview (1 = winner, 0 = loser)
gen current_winner =.

replace current_winner = 1 if prtvtees == 2 & after == 0 
replace current_winner = 0 if prtvtees == 3 & after == 0 
replace current_winner = 0 if prtvtees == 7 & after == 0 
replace current_winner = 0 if prtvtees == 6 & after == 0 

replace current_winner = 0 if prtvtees == 1 & after == 0 
replace current_winner = 0 if prtvtees == 5 & after == 0 
replace current_winner = 0 if prtvtees == 17 & after == 0 
replace current_winner = 0 if prtvtees == 16 & after == 0 
replace current_winner = 0 if prtvtees == 8 & after == 0 
replace current_winner = 0 if prtvtees == 9 & after == 0 
replace current_winner = 0 if prtvtees == 10 & after == 0 
replace current_winner = 0 if prtvtees == 11 & after == 0 
replace current_winner = 0 if prtvtees == 12 & after == 0 
replace current_winner = 0 if prtvtees == 13 & after == 0 
replace current_winner = 0 if prtvtees == 14 & after == 0 
replace current_winner = 0 if prtvtees == 15 & after == 0 
replace current_winner = 0 if prtvtees == 18 & after == 0 
replace current_winner = 0 if prtvtees == 19 & after == 0 
replace current_winner = 0 if prtvtees == 20 & after == 0 
replace current_winner = 0 if prtvtees == 53 & after == 0 

replace current_winner = 1 if prtclfes == 2 & after == 0 
replace current_winner = 0 if prtclfes == 3 & after == 0 
replace current_winner = 0 if prtclfes == 7 & after == 0 
replace current_winner = 0 if prtclfes == 6 & after == 0 

replace current_winner = 0 if prtclfes == 1 & after == 0 
replace current_winner = 0 if prtclfes == 5 & after == 0 
replace current_winner = 0 if prtclfes == 17 & after == 0 
replace current_winner = 0 if prtclfes == 16 & after == 0 
replace current_winner = 0 if prtclfes == 8 & after == 0 
replace current_winner = 0 if prtclfes == 9 & after == 0 
replace current_winner = 0 if prtclfes == 10 & after == 0 
replace current_winner = 0 if prtclfes == 11 & after == 0 
replace current_winner = 0 if prtclfes == 12 & after == 0 
replace current_winner = 0 if prtclfes == 13 & after == 0 
replace current_winner = 0 if prtclfes == 14 & after == 0 
replace current_winner = 0 if prtclfes == 15 & after == 0 
replace current_winner = 0 if prtclfes == 18 & after == 0 
replace current_winner = 0 if prtclfes == 19 & after == 0 
replace current_winner = 0 if prtclfes == 20 & after == 0 
replace current_winner = 0 if prtclfes == 53 & after == 0 

replace current_winner = 1 if prtvtees == 2 & after == 1 
replace current_winner = 1 if prtvtees == 3 & after == 1 
replace current_winner = 1 if prtvtees == 7 & after == 1 
replace current_winner = 1 if prtvtees == 6 & after == 1 

replace current_winner = 0 if prtvtees == 1 & after == 1 
replace current_winner = 0 if prtvtees == 5 & after == 1 
replace current_winner = 0 if prtvtees == 17 & after == 1 
replace current_winner = 0 if prtvtees == 16 & after == 1 
replace current_winner = 0 if prtvtees == 8 & after == 1 
replace current_winner = 0 if prtvtees == 9 & after == 1 
replace current_winner = 0 if prtvtees == 10 & after == 1 
replace current_winner = 0 if prtvtees == 11 & after == 1 
replace current_winner = 0 if prtvtees == 12 & after == 1 
replace current_winner = 0 if prtvtees == 13 & after == 1 
replace current_winner = 0 if prtvtees == 14 & after == 1 
replace current_winner = 0 if prtvtees == 15 & after == 1 
replace current_winner = 0 if prtvtees == 18 & after == 1 
replace current_winner = 0 if prtvtees == 19 & after == 1 
replace current_winner = 0 if prtvtees == 20 & after == 1 
replace current_winner = 0 if prtvtees == 53 & after == 1 

replace current_winner = 1 if prtclfes == 2 & after == 1 
replace current_winner = 1 if prtclfes == 3 & after == 1 
replace current_winner = 1 if prtclfes == 7 & after == 1 
replace current_winner = 1 if prtclfes == 6 & after == 1 

replace current_winner = 0 if prtclfes == 1 & after == 1 
replace current_winner = 0 if prtclfes == 5 & after == 1 
replace current_winner = 0 if prtclfes == 17 & after == 1
replace current_winner = 0 if prtclfes == 16 & after == 1 
replace current_winner = 0 if prtclfes == 8 & after == 1 
replace current_winner = 0 if prtclfes == 9 & after == 1 
replace current_winner = 0 if prtclfes == 10 & after == 1 
replace current_winner = 0 if prtclfes == 11 & after == 1
replace current_winner = 0 if prtclfes == 12 & after == 1 
replace current_winner = 0 if prtclfes == 13 & after == 1 
replace current_winner = 0 if prtclfes == 14 & after == 1
replace current_winner = 0 if prtclfes == 15 & after == 1 
replace current_winner = 0 if prtclfes == 18 & after == 1 
replace current_winner = 0 if prtclfes == 19 & after == 1 
replace current_winner = 0 if prtclfes == 20 & after == 1 
replace current_winner = 0 if prtclfes == 53 & after == 1 

// Save
save "$Data/ESS9, Spain 2.0.dta" , replace

********************************************************************************
* Cyprus Election 2011
********************************************************************************

// Upload dataset:
use "ESS5, Cyprus.dta", clear

// -----------------------------------------------------------------------------
// Date Variables: Calculate the Cut-off Dates and Respondent Interview Dates
// -----------------------------------------------------------------------------

// Define the election day reference date in Stata's date format
gen ref_date_election = mdy(5, 23, 2011)

// Convert the interview date in Stata's date format using "inwyys", "inwmms", and "inwdds"
gen obs_date = mdy(inwmms, inwdds, inwyys)

// Calculate the difference in days between the observation date and the reference date
gen days_election = obs_date - ref_date_election

// Create a dummy variable for whether the interview occurred before or after election day
gen after_election = 0
replace after_election = 1 if days_election>0

// Generate the reference date for the establishment of the election outcome in Stata's date format
gen ref_date = mdy(5, 23, 2011)

// Calculate days between interview date and the date when election results became clear 
gen days = obs_date - ref_date

// Create a dummy variable for observations made before or after the election results solidified
gen after = 0
replace after = 1 if days>0

// -----------------------------------------------------------------------------
// Manual classification of winners and losers
// -----------------------------------------------------------------------------

// Create dummy for electoral winners (1 = supported a party that entered government following the election, 0 = supported a party that remained in or moved to opposition)
gen winner =.

// Assign winner status based on vote in last election (prtvtcy)
replace winner = 1 if prtvtcy == 1 
replace winner = 1 if prtvtcy == 2 
replace winner = 0 if prtvtcy >2 & prtvtcy< 9 

// If available, override with winner status based on party respondent currently feels closest to (prtclcy)
replace winner = 1 if prtclcy == 1 
replace winner = 1 if prtclcy == 2 
replace winner = 0 if prtclcy >2 & prtvtcy< 9 

// Assign winner status at time of the interview (1 = winner, 0 = loser)
gen current_winner =.

replace current_winner = 1 if prtvtcy == 1 
replace current_winner = 1 if prtvtcy == 2 
replace current_winner = 0 if prtvtcy >2 & prtvtcy< 9 

replace current_winner = 1 if prtclcy == 1 
replace current_winner = 1 if prtclcy == 2 
replace current_winner = 0 if prtclcy >2 & prtclcy< 9 

// Save
save "$Data/ESS5, Cyprus 2.0.dta" , replace

********************************************************************************
* Croatia Presidential Election 2019-2020
********************************************************************************

// Upload dataset:
use "ESS9, Croatia.dta", clear

// -----------------------------------------------------------------------------
// Date Variables: Calculate the Cut-off Dates and Respondent Interview Dates
// -----------------------------------------------------------------------------

// Define the election day reference date in Stata's date format
gen ref_date_election = mdy(1, 6, 2020)

// Calculate the observation date in Stata's date format using "inwyys", "inwmms", and "inwdds"
gen obs_date = mdy(inwmms, inwdds, inwyys)

// Calculate the difference in days between the observation date and the reference date
gen days_election = obs_date - ref_date_election

// Create a dummy variable for whether the interview occurred before or after election day
gen after_election = 0
replace after_election = 1 if days_election>0

// Generate the reference date for the establishment of the election outcome in Stata's date format
gen ref_date = mdy(1, 6, 2020)

// Calculate days between interview date and the date when election results became clear 
gen days = obs_date - ref_date

// Create a dummy variable for observations made before or after the election results solidified
gen after = 0
replace after = 1 if days>0

// -----------------------------------------------------------------------------
// Manual classification of winners and losers
// -----------------------------------------------------------------------------

// Create dummy for electoral winners (1 = supported a party that entered government following the election, 0 = supported a party that remained in or moved to opposition)
gen winner =.

// Assign winner status based on vote in last election (prtvtahr)
replace winner = 1 if prtvtahr == 2 
replace winner = 0 if prtvtahr == 1 

// If available, override with winner status based on party respondent currently feels closest to (prtclahr)
replace winner = 1 if prtclahr == 2 
replace winner = 0 if prtclahr == 1 
replace winner = 1 if prtclahr == 6 
replace winner = 1 if prtclahr == 5 

// Assign winner status at time of the interview (1 = winner, 0 = loser)
gen current_winner =.

replace current_winner = 0 if prtvtahr == 2 & after == 0 
replace current_winner = 1 if prtvtahr == 1 & after == 0 

replace current_winner = 1 if prtvtahr == 2 & after == 1 
replace current_winner = 0 if prtvtahr == 1 & after == 1 

replace winner = 0 if prtclahr == 2  & after == 0 
replace winner = 1 if prtclahr == 1  & after == 0 
replace winner = 0 if prtclahr == 6  & after == 0 
replace winner = 0 if prtclahr == 5  & after == 0 

replace winner = 1 if prtclahr == 2  & after == 1 
replace winner = 0 if prtclahr == 1  & after == 1 
replace winner = 1 if prtclahr == 6  & after == 1 
replace winner = 1 if prtclahr == 5  & after == 1 

// Save
save "$Data/ESS9, Croatia 2.0.dta" , replace

********************************************************************************
* Serbia Presidential Election 2022
********************************************************************************

// Upload dataset
use "ESS10SC, Serbia.dta", clear

// -----------------------------------------------------------------------------
// Date Variables: Calculate the Cut-off Dates and Respondent Interview Dates
// -----------------------------------------------------------------------------

// Convert the dates of the observations to discrete values using "questcmp" (date of the interview) 
gen inwds_days = floor((questcmp) / 86400000)

// Calculate days between interview and election day (reference date = 22739 in the inwds_days format)
gen days_election = inwds_days - 22739

// Create a dummy variable for whether the interview occurred before or after election day
gen after_election = 0
replace after_election = 1 if days_election>0

// Calculate days between interview date and the date when election results became clear (reference day: 22739)
gen days = inwds_days - 22739

// Create a dummy variable for observations made before or after the election results solidified
gen after = 0
replace after = 1 if days>0

// -----------------------------------------------------------------------------
// Manual classification of winners and losers
// -----------------------------------------------------------------------------

// Create dummy for electoral winners (1 = supported a party that entered government following the election, 0 = supported a party that remained in or moved to opposition)
gen winner =.

// Assign winner status based on vote in last election (prtvtars)
replace winner = 1 if prtvtars == 1 
replace winner = 0 if prtvtars >1 

// Assign winner status at time of the interview (1 = winner, 0 = loser)
gen current_winner =.

replace current_winner = 1 if prtvtars == 1 
replace current_winner = 0 if prtvtars >1 

// Save
save "$Data/ESS10, Serbia 2.0.dta" , replace

********************************************************************************
* Slovenia 2012 Presidential Election
********************************************************************************

// Upload dataset:
use "ESS6, Slovenia.dta", clear

// -----------------------------------------------------------------------------
// Date Variables: Calculate the Cut-off Dates and Respondent Interview Dates
// -----------------------------------------------------------------------------

// Define the election day reference date in Stata's date format
gen ref_date_election = mdy(12, 3, 2012)

// Calculate the observation date in Stata's date format using "inwyys", "inwmms", and "inwdds"
gen obs_date = mdy(inwmms, inwdds, inwyys)

// Calculate the difference in days between the observation date and the reference date
gen days_election = obs_date - ref_date_election

// Create a dummy variable for whether the interview occurred before or after election day
gen after_election = 0
replace after_election = 1 if days_election>0

// Generate the reference date for the establishment of the election outcome in Stata's date format
gen ref_date = mdy(12, 3, 2012)

// Calculate days between interview date and the date when election results became clear 
gen days = obs_date - ref_date

// Create a dummy variable for observations made before or after the election results solidified
gen after = 0
replace after = 1 if days>0

// -----------------------------------------------------------------------------
// Manual classification of winners and losers
// -----------------------------------------------------------------------------

// Create dummy for electoral winners (1 = supported a party that entered government following the election, 0 = supported a party that remained in or moved to opposition)
gen winner =.

// Assign winner status based on vote in last election (prtvtdsi)
replace winner = 1 if prtvtdsi == 6 
replace winner = 1 if prtvtdsi == 8 

replace winner = 0 if prtvtdsi == 7 
replace winner = 0 if prtvtdsi == 1 
replace winner = 0 if prtvtdsi == 2 

// If available, override with winner status based on party respondent currently feels closest to (prtcldsi)
replace winner = 1 if prtcldsi == 6 
replace winner = 1 if prtcldsi == 8 

replace winner = 0 if prtcldsi == 7 
replace winner = 0 if prtcldsi == 1 
replace winner = 0 if prtcldsi == 2 

// Assign winner status at time of the interview (1 = winner, 0 = loser)
gen current_winner =.

replace current_winner = 1 if prtvtdsi == 6 & after == 0 
replace current_winner = 1 if prtvtdsi == 9 & after == 0  
replace current_winner = 0 if prtvtdsi == 8 & after == 0  

replace current_winner = 1 if prtvtdsi == 7 & after == 0  
replace current_winner = 1 if prtvtdsi == 1 & after == 0  
replace current_winner = 1 if prtvtdsi == 2 & after == 0  

replace current_winner = 1 if prtvtdsi == 6 & after == 1
replace current_winner = 1 if prtvtdsi == 8 & after == 1  
replace current_winner = 0 if prtvtdsi == 9 & after == 1  

replace current_winner = 0 if prtvtdsi == 7 & after == 1  
replace current_winner = 0 if prtvtdsi == 1 & after == 1  
replace current_winner = 0 if prtvtdsi == 2 & after == 1 

replace current_winner = 1 if prtcldsi == 6 & after == 0 
replace current_winner = 1 if prtcldsi == 9 & after == 0  
replace current_winner = 0 if prtcldsi == 8 & after == 0  

replace current_winner = 1 if prtcldsi == 7 & after == 0  
replace current_winner = 1 if prtcldsi == 1 & after == 0  
replace current_winner = 1 if prtcldsi == 2 & after == 0  

replace current_winner = 1 if prtcldsi == 6 & after == 1 
replace current_winner = 1 if prtcldsi == 8 & after == 1  
replace current_winner = 0 if prtcldsi == 9 & after == 1  

replace current_winner = 0 if prtcldsi == 7 & after == 1  
replace current_winner = 0 if prtcldsi == 1 & after == 1  
replace current_winner = 0 if prtcldsi == 2 & after == 1 

// Save
save "$Data/ESS6, Slovenia 2.0.dta" , replace

********************************************************************************
* Portugal 2015 
********************************************************************************

// Upload dataset
use "ESS7, Portugal.dta", clear

// -----------------------------------------------------------------------------
// Date Variables: Calculate the Cut-off Dates and Respondent Interview Dates
// -----------------------------------------------------------------------------

// Define the election day reference date in Stata's date format
gen ref_date_election = mdy(10, 5, 2015)

// Calculate the observation date in Stata's date format using "inwyys", "inwmms", and "inwdds"
gen obs_date = mdy(inwmms, inwdds, inwyys)

// Calculate the difference in days between the observation date and the reference date
gen days_election = obs_date - ref_date_election

// Create a dummy variable for whether the interview occurred before or after election day
gen after_election = 0
replace after_election = 1 if days_election>0

// Generate the reference date for the establishment of the election outcome in Stata's date format
gen ref_date = mdy(11, 27, 2015)

// Calculate days between interview date and the date when election results became clear 
gen days = obs_date - ref_date

// Create a dummy variable for observations made before or after the election results solidified
gen after = 0
replace after = 1 if days>0

// -----------------------------------------------------------------------------
// Manual classification of winners and losers
// -----------------------------------------------------------------------------

// Create dummy for electoral winners (1 = supported a party that entered government following the election, 0 = supported a party that remained in or moved to opposition)
gen winner =.

// Assign winner status based on vote in last election
replace winner = 1 if prtvtbpt == 11 
replace winner = 0 if prtvtbpt == 1 
replace winner = 0 if prtvtbpt == 2 
replace winner = 0 if prtvtbpt == 3 
replace winner = 0 if prtvtbpt == 4 
replace winner = 0 if prtvtbpt == 5 
replace winner = 0 if prtvtbpt == 6 
replace winner = 0 if prtvtbpt == 7 
replace winner = 0 if prtvtbpt == 8 
replace winner = 0 if prtvtbpt == 9 
replace winner = 0 if prtvtbpt == 10 
replace winner = 0 if prtvtbpt == 12 
replace winner = 0 if prtvtbpt == 13 

// If available, override with winner status based on party respondent currently feels closest to
replace winner = 0 if prtcldpt == 1 
replace winner = 0 if prtcldpt == 2 
replace winner = 0 if prtcldpt == 3 
replace winner = 0 if prtcldpt == 4 
replace winner = 0 if prtcldpt == 5 
replace winner = 0 if prtcldpt == 6 
replace winner = 0 if prtcldpt == 7 
replace winner = 0 if prtcldpt == 8 
replace winner = 1 if prtcldpt == 9 

// Assign winner status at time of the interview (1 = winner, 0 = loser)
gen current_winner =.

replace current_winner = 0 if prtvtbpt == 11 & after == 0 
replace current_winner = 0 if prtvtbpt == 1 & after == 0 
replace current_winner = 1 if prtvtbpt == 2 & after == 0 
replace current_winner = 0 if prtvtbpt == 3 & after == 0 
replace current_winner = 0 if prtvtbpt == 4 & after == 0 
replace current_winner = 0 if prtvtbpt == 5 & after == 0 
replace current_winner = 0 if prtvtbpt == 6 & after == 0 
replace current_winner = 0 if prtvtbpt == 7 & after == 0 
replace current_winner = 0 if prtvtbpt == 8 & after == 0 
replace current_winner = 0 if prtvtbpt == 9 & after == 0 
replace current_winner = 1 if prtvtbpt == 10 & after == 0 
replace current_winner = 0 if prtvtbpt == 12 & after == 0 
replace current_winner = 0 if prtvtbpt == 13 & after == 0 

replace current_winner = 0 if prtcldpt == 1  & after == 0 
replace current_winner = 1 if prtcldpt == 2  & after == 0 
replace current_winner = 0 if prtcldpt == 3  & after == 0 
replace current_winner = 0 if prtcldpt == 4  & after == 0 
replace current_winner = 0 if prtcldpt == 5  & after == 0 
replace current_winner = 0 if prtcldpt == 6  & after == 0 
replace current_winner = 0 if prtcldpt == 7  & after == 0 
replace current_winner = 1 if prtcldpt == 8  & after == 0 
replace current_winner = 0 if prtcldpt == 9  & after == 0 

replace current_winner = 1 if prtvtbpt == 11 & after == 1 
replace current_winner = 0 if prtvtbpt == 1 & after == 1 
replace current_winner = 0 if prtvtbpt == 2 & after == 1 
replace current_winner = 0 if prtvtbpt == 3 & after == 1 
replace current_winner = 0 if prtvtbpt == 4 & after == 1 
replace current_winner = 0 if prtvtbpt == 5 & after == 1 
replace current_winner = 0 if prtvtbpt == 6 & after == 1 
replace current_winner = 0 if prtvtbpt == 7 & after == 1 
replace current_winner = 0 if prtvtbpt == 8 & after == 1 
replace current_winner = 0 if prtvtbpt == 9 & after == 1 
replace current_winner = 0 if prtvtbpt == 10 & after == 1 
replace current_winner = 0 if prtvtbpt == 12 & after == 1 
replace current_winner = 0 if prtvtbpt == 13 & after == 1 

replace current_winner = 0 if prtcldpt == 1  & after == 1 
replace current_winner = 0 if prtcldpt == 2  & after == 1 
replace current_winner = 0 if prtcldpt == 3  & after == 1 
replace current_winner = 0 if prtcldpt == 4  & after == 1 
replace current_winner = 0 if prtcldpt == 5  & after == 1 
replace current_winner = 0 if prtcldpt == 6  & after == 1 
replace current_winner = 0 if prtcldpt == 7  & after == 1 
replace current_winner = 0 if prtcldpt == 8  & after == 1 
replace current_winner = 1 if prtcldpt == 9  & after == 1 

// Save
save "$Data/ESS7, Portugal 2.0.dta" , replace

********************************************************************************
* Croatia 2011
********************************************************************************

// Upload dataset:
use "ESS5, Croatia.dta", clear

// -----------------------------------------------------------------------------
// Date Variables: Calculate the Cut-off Dates and Respondent Interview Dates
// -----------------------------------------------------------------------------

// Define the election day reference date in Stata's date format
gen ref_date_election = mdy(12, 5, 2011)

// Calculate the observation date in Stata's date format using "inwyys", "inwmms", and "inwdds"
gen obs_date = mdy(inwmms, inwdds, inwyys)

// Calculate the difference in days between the observation date and the reference date
gen days_election = obs_date - ref_date_election

// Create a dummy variable for whether the interview occurred before or after election day
gen after_election = 0
replace after_election = 1 if days_election>0

// Generate the reference date for the establishment of the election outcome in Stata's date format
gen ref_date = mdy(12, 5, 2011)

// Calculate days between interview date and the date when election results became clear 
gen days = obs_date - ref_date

// Create a dummy variable for observations made before or after the election results solidified
gen after = 0
replace after = 1 if days>0

// -----------------------------------------------------------------------------
// Manual classification of winners and losers
// -----------------------------------------------------------------------------

// Create dummy for electoral winners (1 = supported a party that entered government following the election, 0 = supported a party that remained in or moved to opposition)
gen winner =.

// Assign winner status based on vote in last election
replace winner = 1 if prtvthr == 2 
replace winner = 1 if prtvthr == 3
replace winner = 1 if prtvthr == 6 
replace winner = 1 if prtvthr == 10 

replace winner = 0 if prtvthr == 1 
replace winner = 0 if prtvthr == 4 
replace winner = 0 if prtvthr == 5 
replace winner = 0 if prtvthr == 7
replace winner = 0 if prtvthr == 8 
replace winner = 0 if prtvthr == 9 
replace winner = 0 if prtvthr == 11 

// If available, override with winner status based on party respondent currently feels closest to
replace winner = 1 if prtclhr == 2 
replace winner = 1 if prtclhr == 3 
replace winner = 1 if prtclhr == 6
replace winner = 1 if prtclhr == 10 

replace winner = 0 if prtclhr == 1
replace winner = 0 if prtclhr == 4 
replace winner = 0 if prtclhr == 5 
replace winner = 0 if prtclhr == 7 
replace winner = 0 if prtclhr == 8 
replace winner = 0 if prtclhr == 9 
replace winner = 0 if prtclhr == 11 

// Assign winner status at time of the interview (1 = winner, 0 = loser)
gen current_winner =.

replace current_winner = 0 if prtvthr == 2 & after == 0 
replace current_winner = 0 if prtvthr == 3 & after == 0  
replace current_winner = 0 if prtvthr == 6 & after == 0   
replace current_winner = 0 if prtvthr == 10 & after == 0  

replace current_winner = 1 if prtvthr == 1 & after == 0  
replace current_winner = 1 if prtvthr == 4 & after == 0  
replace current_winner = 1 if prtvthr == 5 & after == 0  
replace current_winner = 0 if prtvthr == 7 & after == 0  
replace current_winner = 1 if prtvthr == 8 & after == 0  
replace current_winner = 0 if prtvthr == 9 & after == 0  
replace current_winner = 0 if prtvthr == 11 & after == 0  

replace current_winner = 0 if prtclhr == 2 & after == 0  
replace current_winner = 0 if prtclhr == 3 & after == 0  
replace current_winner = 0 if prtclhr == 6 & after == 0  
replace current_winner = 0 if prtclhr == 10 & after == 0  

replace current_winner = 1 if prtclhr == 1 & after == 0 
replace current_winner = 1 if prtclhr == 4 & after == 0  
replace current_winner = 1 if prtclhr == 5 & after == 0  
replace current_winner = 0 if prtclhr == 7 & after == 0  
replace current_winner = 1 if prtclhr == 8 & after == 0  
replace current_winner = 0 if prtclhr == 9 & after == 0 
replace current_winner = 0 if prtclhr == 11 & after == 0  

replace current_winner = 1 if prtvthr == 2 & after == 1 
replace current_winner = 1 if prtvthr == 3 & after == 1  
replace current_winner = 1 if prtvthr == 6 & after == 1  
replace current_winner = 1 if prtvthr == 10 & after == 1  

replace current_winner = 0 if prtvthr == 1 & after == 1  
replace current_winner = 0 if prtvthr == 4 & after == 1  
replace current_winner = 0 if prtvthr == 5 & after == 1  
replace current_winner = 0 if prtvthr == 7 & after == 1  
replace current_winner = 0 if prtvthr == 8 & after == 1  
replace current_winner = 0 if prtvthr == 9 & after == 1  
replace current_winner = 0 if prtvthr == 11 & after == 1  

replace current_winner = 1 if prtclhr == 2 & after == 1 
replace current_winner = 1 if prtclhr == 3 & after == 1 
replace current_winner = 1 if prtclhr == 6 & after == 1  
replace current_winner = 1 if prtclhr == 10 & after == 1  

replace current_winner = 0 if prtclhr == 1 & after == 1  
replace current_winner = 0 if prtclhr == 4 & after == 1  
replace current_winner = 0 if prtclhr == 5 & after == 1  
replace current_winner = 0 if prtclhr == 7 & after == 1  
replace current_winner = 0 if prtclhr == 8 & after == 1  
replace current_winner = 0 if prtclhr == 9 & after == 1  
replace current_winner = 0 if prtclhr == 11 & after == 1  

// Save
save "$Data/ESS5, Croatia 2.0.dta" , replace

********************************************************************************
* Estonia 2007 
********************************************************************************

// Upload dataset
use "ESS3, Estonia.dta", clear

// -----------------------------------------------------------------------------
// Date Variables: Calculate the Cut-off Dates and Respondent Interview Dates
// -----------------------------------------------------------------------------

// Define the election day reference date in Stata's date format
gen ref_date_election = mdy(3, 5, 2007)

// Calculate the observation date in Stata's date format using "inwyys", "inwmms", and "inwdds"
gen obs_date = mdy(inwmms, inwdds, inwyys)

// Calculate the difference in days between the observation date and the reference date
gen days_election = obs_date - ref_date_election

// Create a dummy variable for whether the interview occurred before or after election day
gen after_election = 0
replace after_election = 1 if days_election>0

// Generate the reference date for the establishment of the election outcome in Stata's date format
gen ref_date = mdy(4, 5, 2007)

// Calculate days between interview date and the date when election results became clear 
gen days = obs_date - ref_date

// Create a dummy variable for observations made before or after the election results solidified
gen after = 0
replace after = 1 if days>0

// -----------------------------------------------------------------------------
// Manual classification of winners and losers
// -----------------------------------------------------------------------------

// Create dummy for electoral winners (1 = supported a party that entered government following the election, 0 = supported a party that remained in or moved to opposition)
gen winner =.

// Assign winner status based on vote in last election
replace winner = 1 if prtvtaee == 1 
replace winner = 1 if prtvtaee == 3 
replace winner = 1 if prtvtaee == 5 

replace winner = 0 if prtvtaee == 2 
replace winner = 0 if prtvtaee == 4 
replace winner = 0 if prtvtaee == 6 
replace winner = 0 if prtvtaee == 7 

// If available, override with winner status based on party respondent currently feels closest to
replace winner = 1 if prtclaee == 1 
replace winner = 1 if prtclaee == 3 
replace winner = 1 if prtclaee == 5
 
replace winner = 0 if prtclaee == 2 
replace winner = 0 if prtclaee == 4 
replace winner = 0 if prtclaee == 6 
replace winner = 0 if prtclaee == 7 

// Assign winner status at time of the interview (1 = winner, 0 = loser)
gen current_winner =.

replace current_winner = 1 if prtvtaee == 2 & after == 0
replace current_winner = 1 if prtvtaee == 3 & after == 0
replace current_winner = 1 if prtvtaee == 6 & after == 0

replace current_winner = 0 if prtvtaee == 1 & after == 0
replace current_winner = 0 if prtvtaee == 4 & after == 0
replace current_winner = 0 if prtvtaee == 5 & after == 0
replace current_winner = 0 if prtvtaee == 7 & after == 0

replace current_winner = 1 if prtclaee == 2 & after == 0
replace current_winner = 1 if prtclaee == 3 & after == 0
replace current_winner = 1 if prtclaee == 6 & after == 0

replace current_winner = 0 if prtclaee == 1 & after == 0
replace current_winner = 0 if prtclaee == 4 & after == 0
replace current_winner = 0 if prtclaee == 5 & after == 0
replace current_winner = 0 if prtclaee == 7 & after == 0

replace current_winner = 1 if prtvtaee == 1 & after == 1
replace current_winner = 1 if prtvtaee == 3 & after == 1
replace current_winner = 1 if prtvtaee == 5 & after == 1

replace current_winner = 0 if prtvtaee == 2 & after == 1
replace current_winner = 0 if prtvtaee == 4 & after == 1
replace current_winner = 0 if prtvtaee == 6 & after == 1
replace current_winner = 0 if prtvtaee == 7 & after == 1

replace current_winner = 1 if prtclaee == 1 & after == 1
replace current_winner = 1 if prtclaee == 3 & after == 1
replace current_winner = 1 if prtclaee == 5 & after == 1
 
replace current_winner = 0 if prtclaee == 2 & after == 1
replace current_winner = 0 if prtclaee == 4 & after == 1
replace current_winner = 0 if prtclaee == 6 & after == 1
replace current_winner = 0 if prtclaee == 7 & after == 1

// Save
save "$Data/ESS3, Estonia 2.0.dta" , replace

********************************************************************************
* Portugal 2022 
********************************************************************************

// Upload dataset:
use "ESS10, Portugal.dta", clear

// -----------------------------------------------------------------------------
// Date Variables: Calculate the Cut-off Dates and Respondent Interview Dates
// -----------------------------------------------------------------------------

// Generate discrete values for for the date of each observations using "inwds" (date of the interview) 
gen inwds_days = floor((inwds) / 86400000)

// Define the election day (Election day: 22675)
gen days_election = inwds_days - 22675

// Create a dummy variable for whether the interview occurred before or after election day
gen after_election = 0
replace after_election = 1 if days_election>0

// Calculate days between interview date and the date when election results became clear (reference day: 22675)
gen days = inwds_days - 22675

// Create a dummy variable for observations made before or after the election results solidified
gen after = 0
replace after = 1 if days>0

// -----------------------------------------------------------------------------
// Manual classification of winners and losers
// -----------------------------------------------------------------------------

// Create dummy for electoral winners (1 = supported a party that entered government following the election, 0 = supported a party that remained in or moved to opposition)
gen winner =.

// Assign winner status based on vote in last election
replace winner = 1 if prtvtdpt == 18 
replace winner = 0 if prtvtdpt == 16 
replace winner = 0 if prtvtdpt == 4 
replace winner = 0 if prtvtdpt == 5 
replace winner = 0 if prtvtdpt == 12 
replace winner = 0 if prtvtdpt == 2 
replace winner = 0 if prtvtdpt == 11 
replace winner = 0 if prtvtdpt == 7 

// If available, override with winner status based on party respondent currently feels closest to
replace winner = 1 if prtclfpt == 21 
replace winner = 0 if prtclfpt == 20 
replace winner = 0 if prtclfpt == 4 
replace winner = 0 if prtclfpt == 5 
replace winner = 0 if prtclfpt == 23 
replace winner = 0 if prtclfpt == 2 
replace winner = 0 if prtclfpt == 18 
replace winner = 0 if prtclfpt == 7 

// Assign winner status at time of the interview (1 = winner, 0 = loser)
gen current_winner =.

replace current_winner = 1 if prtvtdpt == 18 
replace current_winner = 0 if prtvtdpt == 16 
replace current_winner = 0 if prtvtdpt == 4 
replace current_winner = 0 if prtvtdpt == 5 
replace current_winner = 0 if prtvtdpt == 12 
replace current_winner = 0 if prtvtdpt == 2 
replace current_winner = 0 if prtvtdpt == 11 
replace current_winner = 0 if prtvtdpt == 7

replace current_winner = 1 if prtclfpt == 21 
replace current_winner = 0 if prtclfpt == 20 
replace current_winner = 0 if prtclfpt == 4
replace current_winner = 0 if prtclfpt == 5 
replace current_winner = 0 if prtclfpt == 23 
replace current_winner = 0 if prtclfpt == 2 
replace current_winner = 0 if prtclfpt == 18 
replace current_winner = 0 if prtclfpt == 7 

// Save
save "$Data/ESS10, Portugal 2.0.dta" , replace

********************************************************************************
* Sweden 2014 
********************************************************************************

// Upload dataset
use "ESS7, Sweden.dta", clear

// -----------------------------------------------------------------------------
// Date Variables: Calculate the Cut-off Dates and Respondent Interview Dates
// -----------------------------------------------------------------------------

// Define the election day reference date in Stata's date format
gen ref_date_election = mdy(9, 14, 2014)

// Calculate the observation date in Stata's date format using "inwyys", "inwmms", and "inwdds"
gen obs_date = mdy(inwmms, inwdds, inwyys)

// Calculate the difference in days between the observation date and the reference date
gen days_election = obs_date - ref_date_election

// Create a dummy variable for whether the interview occurred before or after election day
gen after_election = 0
replace after_election = 1 if days_election>0

// Generate the reference date for the establishment of the election outcome in Stata's date format
gen ref_date = mdy(9, 14, 2014)

// Calculate days between interview date and the date when election results became clear 
gen days = obs_date - ref_date

// Create a dummy variable for observations made before or after the election results solidified
gen after = 0
replace after = 1 if days>0

// -----------------------------------------------------------------------------
// Manual classification of winners and losers
// -----------------------------------------------------------------------------

// Create dummy for electoral winners (1 = supported a party that entered government following the election, 0 = supported a party that remained in or moved to opposition)
gen winner =.

// Assign winner status based on vote in last election
replace winner = 1 if prtvtbse == 4 
replace winner = 1 if prtvtbse == 6 
replace winner = 0 if prtvtbse == 1 
replace winner = 0 if prtvtbse == 2 
replace winner = 0 if prtvtbse == 3 
replace winner = 0 if prtvtbse == 5 
replace winner = 0 if prtvtbse == 7 
replace winner = 0 if prtvtbse == 8 
replace winner = 0 if prtvtbse == 9 
replace winner = 0 if prtvtbse == 10 

// If available, override with winner status based on party respondent currently feels closest to
replace winner = 1 if prtclbse == 4 
replace winner = 1 if prtclbse == 6 
replace winner = 0 if prtclbse == 1 
replace winner = 0 if prtclbse == 2 
replace winner = 0 if prtclbse == 3 
replace winner = 0 if prtclbse == 5 
replace winner = 0 if prtclbse == 7 
replace winner = 0 if prtclbse == 8 
replace winner = 0 if prtclbse == 9 
replace winner = 0 if prtclbse == 10 

// Assign winner status at time of the interview (1 = winner, 0 = loser)
gen current_winner =.

replace current_winner = 0 if prtvtbse == 4 & after == 0
replace current_winner = 0 if prtvtbse == 6 & after == 0 
replace current_winner = 1 if prtvtbse == 1 & after == 0 
replace current_winner = 1 if prtvtbse == 2 & after == 0 
replace current_winner = 1 if prtvtbse == 3 & after == 0 
replace current_winner = 1 if prtvtbse == 5 & after == 0 
replace current_winner = 0 if prtvtbse == 7 & after == 0 
replace current_winner = 0 if prtvtbse == 8 & after == 0 
replace current_winner = 0 if prtvtbse == 9 & after == 0 
replace current_winner = 0 if prtvtbse == 10 & after == 0 

replace current_winner = 0 if prtclbse == 4 & after == 0 
replace current_winner = 0 if prtclbse == 6 & after == 0 
replace current_winner = 1 if prtclbse == 1 & after == 0 
replace current_winner = 1 if prtclbse == 2 & after == 0 
replace current_winner = 1 if prtclbse == 3 & after == 0 
replace current_winner = 1 if prtclbse == 5 & after == 0 
replace current_winner = 0 if prtclbse == 7 & after == 0 
replace current_winner = 0 if prtclbse == 8 & after == 0 
replace current_winner = 0 if prtclbse == 9 & after == 0 
replace current_winner = 0 if prtclbse == 10 & after == 0 

replace current_winner = 1 if prtvtbse == 4 & after == 1
replace current_winner = 1 if prtvtbse == 6 & after == 1 
replace current_winner = 0 if prtvtbse == 1 & after == 1 
replace current_winner = 0 if prtvtbse == 2 & after == 1 
replace current_winner = 0 if prtvtbse == 3 & after == 1 
replace current_winner = 0 if prtvtbse == 5 & after == 1 
replace current_winner = 0 if prtvtbse == 7 & after == 1 
replace current_winner = 0 if prtvtbse == 8 & after == 1 
replace current_winner = 0 if prtvtbse == 9 & after == 1 
replace current_winner = 0 if prtvtbse == 10 & after == 1 

replace current_winner = 1 if prtclbse == 4 & after == 1 
replace current_winner = 1 if prtclbse == 6 & after == 1 
replace current_winner = 0 if prtclbse == 1 & after == 1 
replace current_winner = 0 if prtclbse == 2 & after == 1 
replace current_winner = 0 if prtclbse == 3 & after == 1 
replace current_winner = 0 if prtclbse == 5 & after == 1 
replace current_winner = 0 if prtclbse == 7 & after == 1 
replace current_winner = 0 if prtclbse == 8 & after == 1 
replace current_winner = 0 if prtclbse == 9 & after == 1 
replace current_winner = 0 if prtclbse == 10 & after == 1 

// Save
save "$Data/ESS7, Sweden 2.0.dta" , replace

********************************************************************************
* Portugal 2005
********************************************************************************

// Upload dataset
use "ESS2, Portugal.dta", clear

// -----------------------------------------------------------------------------
// Date Variables: Calculate the Cut-off Dates and Respondent Interview Dates
// -----------------------------------------------------------------------------

// Define the election day reference date in Stata's date format
gen ref_date_election = mdy(2, 20, 2005)

// Calculate the observation date in Stata's date format using "inwyr" (year), "inwmm" (month), and "inwdd" (date)
gen obs_date = mdy(inwmm, inwdd, inwyr)

// Calculate the difference in days between the observation date and the reference date
gen days_election = obs_date - ref_date_election

// Create a dummy variable for whether the interview occurred before or after election day
gen after_election = 0
replace after_election = 1 if days_election>0

// Generate the reference date for the establishment of the election outcome in Stata's date format
gen ref_date = mdy(2, 20, 2005)

// Calculate days between interview date and the date when election results became clear 
gen days = obs_date - ref_date

// Create a dummy variable for observations made before or after the election results solidified
gen after = 0
replace after = 1 if days>0

// -----------------------------------------------------------------------------
// Manual classification of winners and losers
// -----------------------------------------------------------------------------

// Create dummy for electoral winners (1 = supported a party that entered government following the election, 0 = supported a party that remained in or moved to opposition)
gen winner =.

// Assign winner status based on vote in last election
replace winner = 1 if prtvtpt == 10 
replace winner = 0 if prtvtpt == 11 
replace winner = 0 if prtvtpt == 5 
replace winner = 0 if prtvtpt == 2 
replace winner = 0 if prtvtpt == 1 

// If available, override with winner status based on party respondent currently feels closest to
replace winner = 1 if prtclapt == 10
replace winner = 0 if prtclapt == 5
replace winner = 0 if prtclapt == 2
replace winner = 0 if prtclapt == 1
replace winner = 0 if prtclapt == 12

// Assign winner status at time of the interview (1 = winner, 0 = loser)
gen current_winner =.

replace current_winner = 0 if prtvtpt == 10 & after==0
replace current_winner = 1 if prtvtpt == 11 & after==0
replace current_winner = 0 if prtvtpt == 5 & after==0
replace current_winner = 1 if prtvtpt == 2 & after==0
replace current_winner = 0 if prtvtpt == 1 & after==0

replace current_winner = 0 if prtclapt == 10 & after==0
replace current_winner = 0 if prtclapt == 5 & after==0
replace current_winner = 1 if prtclapt == 2 & after==0
replace current_winner = 1 if prtclapt == 1 & after==0
replace current_winner = 0 if prtclapt == 12 & after==0

replace current_winner = 1 if prtvtpt == 10 & after==1
replace current_winner = 0 if prtvtpt == 11 & after==1
replace current_winner = 0 if prtvtpt == 5 & after==1
replace current_winner = 0 if prtvtpt == 2 & after==1
replace current_winner = 0 if prtvtpt == 1 & after==1

replace current_winner = 1 if prtclapt == 10 & after==1
replace current_winner = 0 if prtclapt == 5 & after==1
replace current_winner = 0 if prtclapt == 2 & after==1
replace current_winner = 0 if prtclapt == 1 & after==1
replace current_winner = 0 if prtclapt == 12 & after==1

// Save
save "$Data/ESS2, Portugal 2.0.dta" , replace

********************************************************************************
* Italy 2006
********************************************************************************

// Upload dataset
use "ESS2, Italy.dta", clear

// -----------------------------------------------------------------------------
// Date Variables: Calculate the Cut-off Dates and Respondent Interview Dates
// -----------------------------------------------------------------------------

// Define the election day reference date in Stata's date format
gen ref_date_election = mdy(4, 10, 2006)

// Calculate the observation date in Stata's date format using "inwyr" (year), "inwmm" (month), and "inwdd" (date)
gen obs_date = mdy(inwmm, inwdd, inwyr)

// Calculate the difference in days between the observation date and the reference date
gen days_election = obs_date - ref_date_election

// Create a dummy variable for whether the interview occurred before or after election day
gen after_election = 0
replace after_election = 1 if days_election>0

// Generate the reference date for the establishment of the election outcome in Stata's date format
gen ref_date = mdy(4, 19, 2006)

// Calculate days between interview date and the date when election results became clear 
gen days = obs_date - ref_date

// Create a dummy variable for observations made before or after the election results solidified
gen after = 0
replace after = 1 if days>0

// -----------------------------------------------------------------------------
// Manual classification of winners and losers
// -----------------------------------------------------------------------------

// Create dummy for electoral winners (1 = supported a party that entered government following the election, 0 = supported a party that remained in or moved to opposition)
gen winner =.

// Assign winner status based on vote in last election
replace winner = 1 if prtvtait == 1 
replace winner = 1 if prtvtait == 2
replace winner = 1 if prtvtait == 3
replace winner = 1 if prtvtait == 4
replace winner = 1 if prtvtait == 6
replace winner = 1 if prtvtait == 7
replace winner = 1 if prtvtait == 13
replace winner = 1 if prtvtait == 19

replace winner = 0 if prtvtait == 8
replace winner = 0 if prtvtait == 9
replace winner = 0 if prtvtait == 10
replace winner = 0 if prtvtait == 11
replace winner = 0 if prtvtait == 12

// If available, override with winner status based on party respondent currently feels closest to
replace winner = 1 if prtclait == 1 
replace winner = 1 if prtclait == 2
replace winner = 1 if prtclait == 3
replace winner = 1 if prtclait == 4
replace winner = 1 if prtclait == 6
replace winner = 1 if prtclait == 7
replace winner = 1 if prtclait == 13
replace winner = 1 if prtclait == 19

replace winner = 0 if prtclait == 8
replace winner = 0 if prtclait == 9
replace winner = 0 if prtclait == 10
replace winner = 0 if prtclait == 11
replace winner = 0 if prtclait == 12
replace winner = 0 if prtclait == 16

// Assign winner status at time of the interview (1 = winner, 0 = loser)
gen current_winner =.

replace current_winner = 1 if prtclait == 1 & after==1
replace current_winner = 1 if prtclait == 2 & after==1
replace current_winner = 1 if prtclait == 3 & after==1
replace current_winner = 1 if prtclait == 4 & after==1
replace current_winner = 1 if prtclait == 6 & after==1
replace current_winner = 1 if prtclait == 7 & after==1
replace current_winner = 1 if prtclait == 13 & after==1
replace current_winner = 1 if prtclait == 19 & after==1

replace current_winner = 0 if prtclait == 8 & after==1
replace current_winner = 0 if prtclait == 9 & after==1
replace current_winner = 0 if prtclait == 10 & after==1
replace current_winner = 0 if prtclait == 11 & after==1
replace current_winner = 0 if prtclait == 12 & after==1

replace current_winner = 0 if prtclait == 1 & after==0
replace current_winner = 0 if prtclait == 2 & after==0
replace current_winner = 0 if prtclait == 3 & after==0
replace current_winner = 0 if prtclait == 4 & after==0
replace current_winner = 0 if prtclait == 6 & after==0
replace current_winner = 0 if prtclait == 7 & after==0
replace current_winner = 0 if prtclait == 13 & after==0
replace current_winner = 0 if prtclait == 19 & after==0

replace current_winner = 1 if prtclait == 8 & after==0
replace current_winner = 1 if prtclait == 9 & after==0
replace current_winner = 1 if prtclait == 10 & after==0
replace current_winner = 1 if prtclait == 11 & after==0
replace current_winner = 1 if prtclait == 12 & after==0

// Save
save "$Data/ESS2, Italy 2.0.dta" , replace

********************************************************************************
* Greece 2009
********************************************************************************

// Upload dataset:
use "ESS4, Greece.dta", clear

// -----------------------------------------------------------------------------
// Date Variables: Calculate the Cut-off Dates and Respondent Interview Dates
// -----------------------------------------------------------------------------

// Define the election day reference date in Stata's date format
gen ref_date_election = mdy(10, 4, 2009)

// Calculate the observation date in Stata's date format using "inwyys", "inwmms", and "inwdds"
gen obs_date = mdy(inwmms, inwdds, inwyys)

// Calculate the difference in days between the observation date and the reference date
gen days_election = obs_date - ref_date_election

// Create a dummy variable for whether the interview occurred before or after election day
gen after_election = 0
replace after_election = 1 if days>0

// Generate the reference date for the establishment of the election outcome in Stata's date format
gen ref_date = mdy(10, 4, 2009)

// Calculate days between interview date and the date when election results became clear 
gen days = obs_date - ref_date

// Create a dummy variable for observations made before or after the election results solidified
gen after = 0
replace after = 1 if days>0

// -----------------------------------------------------------------------------
// Manual classification of winners and losers
// -----------------------------------------------------------------------------

// Create dummy for electoral winners (1 = supported a party that entered government following the election, 0 = supported a party that remained in or moved to opposition)
gen winner =.

// Assign winner status based on vote in last election
replace winner = 1 if prtvtbgr == 1 
replace winner = 0 if prtvtbgr == 2 

// If available, override with winner status based on party respondent currently feels closest to
replace winner = 1 if prtclbgr == 1 
replace winner = 0 if prtclbgr == 2 

// Assign winner status at time of the interview (1 = winner, 0 = loser)
gen current_winner =.

replace current_winner = 1 if prtclbgr == 1 & after==1
replace current_winner = 0 if prtclbgr == 2 & after==1

replace current_winner = 0 if prtclbgr == 1 & after==0
replace current_winner = 1 if prtclbgr == 2 & after==0

// Save
save "$Data/ESS4, Greece 2.0.dta" , replace

********************************************************************************
* Iceland 2021
********************************************************************************

// Upload dataset:
use "ESS10, Iceland.dta", clear

// -----------------------------------------------------------------------------
// Date Variables: Calculate the Cut-off Dates and Respondent Interview Dates
// -----------------------------------------------------------------------------

// Convert the dates of the observations to discrete values using "inwds" (date of the interview) 
gen inwds_days = floor((inwds) / 86400000)

// Calculate days between interview and election day (reference date = 22548 in the inwds_days format)
gen days_election = inwds_days - 22548

// Create a dummy variable for whether the interview occurred before or after election day
gen after_election = 0
replace after_election = 1 if days_election>0

// Calculate days between interview date and the date when election results became clear (reference day: 22548)
gen days = inwds_days - 22548

// Create a dummy variable for observations made before or after the election results solidified
gen after = 0
replace after = 1 if days>0

// -----------------------------------------------------------------------------
// Manual classification of winners and losers
// -----------------------------------------------------------------------------

// Create dummy for electoral winners (1 = supported a party that entered government following the election, 0 = supported a party that remained in or moved to opposition)
gen winner =.

// Assign winner status based on vote in last election
replace winner = 1 if prtvtdis == 9 
replace winner = 1 if prtvtdis == 5 
replace winner = 1 if prtvtdis == 11 

replace winner = 0 if prtvtdis == 1 
replace winner = 0 if prtvtdis == 2 
replace winner = 0 if prtvtdis == 3 
replace winner = 0 if prtvtdis == 4 
replace winner = 0 if prtvtdis == 6 
replace winner = 0 if prtvtdis == 7 
replace winner = 0 if prtvtdis == 8 
replace winner = 0 if prtvtdis == 10 
replace winner = 0 if prtvtdis == 11 
replace winner = 0 if prtvtdis == 12 
replace winner = 0 if prtvtdis == 13 
replace winner = 0 if prtvtdis == 14 
replace winner = 0 if prtvtdis == 15 

// If available, override with winner status based on party respondent currently feels closest to
replace winner = 1 if prtcldis == 9 
replace winner = 1 if prtcldis == 5 
replace winner = 1 if prtcldis == 11 

replace winner = 0 if prtcldis == 1 
replace winner = 0 if prtcldis == 2 
replace winner = 0 if prtcldis == 3 
replace winner = 0 if prtcldis == 4 
replace winner = 0 if prtcldis == 6 
replace winner = 0 if prtcldis == 7 
replace winner = 0 if prtcldis == 8 
replace winner = 0 if prtcldis == 10 
replace winner = 0 if prtcldis == 11 
replace winner = 0 if prtcldis == 12 
replace winner = 0 if prtcldis == 13 
replace winner = 0 if prtcldis == 14 
replace winner = 0 if prtcldis == 15 

// Assign winner status at time of the interview (1 = winner, 0 = loser)
gen current_winner =.

replace current_winner = 1 if prtcldis == 9 & after == 1 
replace current_winner = 1 if prtcldis == 5 & after == 1 
replace current_winner = 1 if prtcldis == 11 & after == 1 

replace current_winner = 0 if prtcldis == 1 
replace current_winner = 0 if prtcldis == 2 
replace current_winner = 0 if prtcldis == 3 
replace current_winner = 0 if prtcldis == 4 
replace current_winner = 0 if prtcldis == 6 
replace current_winner = 0 if prtcldis == 7 
replace current_winner = 0 if prtcldis == 8 
replace current_winner = 0 if prtcldis == 10 
replace current_winner = 0 if prtcldis == 11 
replace current_winner = 0 if prtcldis == 12 
replace current_winner = 0 if prtcldis == 13 
replace current_winner = 0 if prtcldis == 14 
replace current_winner = 0 if prtcldis == 15 

// Generate variable for year
gen inwyys=.
replace inwyys = 2021 if cntry=="IS"

// Save
save "$Data/ESS10, Iceland 2.0.dta" , replace

********************************************************************************
* Poland 2015
********************************************************************************

// Upload dataset
use "ESS7, Poland.dta", clear

// -----------------------------------------------------------------------------
// Date Variables: Calculate the Cut-off Dates and Respondent Interview Dates
// -----------------------------------------------------------------------------

// Define the election day reference date in Stata's date format
gen ref_date_election = mdy(5, 24, 2015)

// Calculate the observation date in Stata's date format using "inwyys", "inwmms", and "inwdds"
gen obs_date = mdy(inwmms, inwdds, inwyys)

// Calculate the difference in days between the observation date and the reference date
gen days_election = obs_date - ref_date_election

// Create a dummy variable for whether the interview occurred before or after election day
gen after_election = 0
replace after_election = 1 if days_election>0

// Generate the reference date for the establishment of the election outcome in Stata's date format
gen ref_date = mdy(5, 24, 2015)

// Calculate days between interview date and the date when election results became clear 
gen days = obs_date - ref_date

// Create a dummy variable for observations made before or after the election results solidified
gen after = 0
replace after = 1 if days>0

// -----------------------------------------------------------------------------
// Manual classification of winners and losers
// -----------------------------------------------------------------------------

// Create dummy for electoral winners (1 = supported a party that entered government following the election, 0 = supported a party that remained in or moved to opposition)
gen winner =.

// Assign winner status based on vote in last election
replace winner = 1 if prtvtcpl == 1 
replace winner = 0 if prtvtcpl == 2 
replace winner = 1 if prtvtcpl == 3 
replace winner = 0 if prtvtcpl == 4 
replace winner = 0 if prtvtcpl == 5 
replace winner = 1 if prtvtcpl == 6 
replace winner = 0 if prtvtcpl == 7 
replace winner = 0 if prtvtcpl == 8 

// If available, override with winner status based on party respondent currently feels closest to
replace winner = 1 if prtclfpl == 1 
replace winner = 0 if prtclfpl == 2 
replace winner = 1 if prtclfpl == 3 
replace winner = 0 if prtclfpl == 4 
replace winner = 1 if prtclfpl == 5 
replace winner = 1 if prtclfpl == 6 
replace winner = 0 if prtclfpl == 7 
replace winner = 0 if prtclfpl == 8 

// Assign winner status at time of the interview (1 = winner, 0 = loser)
gen current_winner =.

replace current_winner = 1 if prtclfpl == 1 & after==1
replace current_winner = 0 if prtclfpl == 2 & after==1
replace current_winner = 1 if prtclfpl == 3 & after==1
replace current_winner = 0 if prtclfpl == 4 & after==1
replace current_winner = 1 if prtclfpl == 5 & after==1
replace current_winner = 1 if prtclfpl == 6 & after==1
replace current_winner = 0 if prtclfpl == 7 & after==1 
replace current_winner = 0 if prtclfpl == 8 & after==1 

replace current_winner = 0 if prtclfpl == 1 & after==0
replace current_winner = 1 if prtclfpl == 2 & after==0
replace current_winner = 0 if prtclfpl == 3 & after==0
replace current_winner = 1 if prtclfpl == 4 & after==0
replace current_winner = 0 if prtclfpl == 5 & after==0
replace current_winner = 0 if prtclfpl == 6 & after==0
replace current_winner = 1 if prtclfpl == 7 & after==0 
replace current_winner = 1 if prtclfpl == 8 & after==1 

// Save
save "$Data/ESS7, Poland 2.0.dta" , replace

********************************************************************************
* Austria 2016
********************************************************************************

// Upload dataset
use "ESS8, Austria.dta", clear

// -----------------------------------------------------------------------------
// Date Variables: Calculate the Cut-off Dates and Respondent Interview Dates
// -----------------------------------------------------------------------------

// Define the election day reference date in Stata's date format
gen ref_date_election = mdy(12, 4, 2016)

// Calculate the observation date in Stata's date format using "inwyys", "inwmms", and "inwdds"
gen obs_date = mdy(inwmms, inwdds, inwyys)

// Calculate the difference in days between the observation date and the reference date
gen days_election = obs_date - ref_date_election

// Create a dummy variable for whether the interview occurred before or after election day
gen after_election = 0
replace after_election = 1 if days_election>0

// Generate the reference date for the establishment of the election outcome in Stata's date format
gen ref_date = mdy(12, 4, 2016)

// Calculate days between interview date and the date when election results became clear 
gen days = obs_date - ref_date

// Create a dummy variable for observations made before or after the election results solidified
gen after = 0
replace after = 1 if days>0

// -----------------------------------------------------------------------------
// Manual classification of winners and losers
// -----------------------------------------------------------------------------

// Create dummy for electoral winners (1 = supported a party that entered government following the election, 0 = supported a party that remained in or moved to opposition)
gen winner =.

// Assign winner status based on vote in last election
replace winner = 1 if prtvtbat == 1 
replace winner = 0 if prtvtbat == 3 
replace winner = 1 if prtvtbat == 5 

// If available, override with winner status based on party respondent currently feels closest to
replace winner = 1 if prtclcat == 1 
replace winner = 0 if prtclcat == 3 
replace winner = 1 if prtclcat == 5 

// Assign winner status at time of the interview (1 = winner, 0 = loser)
gen current_winner =.

replace current_winner = 1 if prtclcat == 1 & after==1
replace current_winner = 0 if prtclcat == 3 & after==1
replace current_winner = 1 if prtclcat == 5 & after==1

replace current_winner = 1 if prtclcat == 1 & after==0
replace current_winner = 0 if prtclcat == 3 & after==0
replace current_winner = 1 if prtclcat == 5 & after==0

// Save:
save "$Data/ESS8, Austria 2.0.dta" , replace

********************************************************************************
* Norway 2021
********************************************************************************

// Upload dataset:
use "ESS10, Norway.dta", clear

// -----------------------------------------------------------------------------
// Date Variables: Calculate the Cut-off Dates and Respondent Interview Dates
// -----------------------------------------------------------------------------

// Convert the dates of the observations to discrete values using "inwds" (date of the interview) 
gen inwds_days = floor((inwds) / 86400000)

// Calculate days between interview and election day (reference date = 22536 in the inwds_days format)
gen days_election = inwds_days - 22536

// Create a dummy variable for whether the interview occurred before or after election day
gen after_election = 0
replace after_election = 1 if days_election>0

// Calculate days between interview date and the date when election results became clear (reference day: 22536)
gen days = inwds_days - 22536

// Create a dummy variable for observations made before or after the election results solidified
gen after = 0
replace after = 1 if days>0

// -----------------------------------------------------------------------------
// Manual classification of winners and losers
// -----------------------------------------------------------------------------

// Create dummy for electoral winners (1 = supported a party that entered government following the election, 0 = supported a party that remained in or moved to opposition)
gen winner =.

// Assign winner status based on vote in last election
replace winner = 1 if prtvtbno == 3 
replace winner = 1 if prtvtbno == 6 

replace winner = 0 if prtvtbno == 1 
replace winner = 0 if prtvtbno == 2 
replace winner = 0 if prtvtbno == 4 
replace winner = 0 if prtvtbno == 5 
replace winner = 0 if prtvtbno == 6 
replace winner = 0 if prtvtbno == 7 
replace winner = 0 if prtvtbno == 8 
replace winner = 0 if prtvtbno == 9 
replace winner = 0 if prtvtbno == 10 
replace winner = 0 if prtvtbno == 11 

// If available, override with winner status based on party respondent currently feels closest to
replace winner = 1 if prtclbno == 3 
replace winner = 1 if prtclbno == 6 

replace winner = 0 if prtclbno == 1 
replace winner = 0 if prtclbno == 2 
replace winner = 0 if prtclbno == 4 
replace winner = 0 if prtclbno == 5 
replace winner = 0 if prtclbno == 6 
replace winner = 0 if prtclbno == 7 
replace winner = 0 if prtclbno == 8 
replace winner = 0 if prtclbno == 9 
replace winner = 0 if prtclbno == 10 
replace winner = 0 if prtclbno == 11 

// Assign winner status at time of the interview (1 = winner, 0 = loser)
gen current_winner =.

replace current_winner = 1 if prtclbno == 3 & after == 1
replace current_winner = 1 if prtclbno == 6 & after == 1

replace current_winner = 0 if prtclbno == 1 & after == 1
replace current_winner = 0 if prtclbno == 2 & after == 1
replace current_winner = 0 if prtclbno == 4 & after == 1
replace current_winner = 0 if prtclbno == 5 & after == 1
replace current_winner = 0 if prtclbno == 6 & after == 1
replace current_winner = 0 if prtclbno == 7 & after == 1
replace current_winner = 0 if prtclbno == 8 & after == 1
replace current_winner = 0 if prtclbno == 9 & after == 1
replace current_winner = 0 if prtclbno == 10 & after == 1
replace current_winner = 0 if prtclbno == 11 & after == 1

replace current_winner = 1 if prtclbno == 7 & after == 0
replace current_winner = 1 if prtclbno == 4 & after == 0
replace current_winner = 1 if prtclbno == 5 & after == 0

replace current_winner = 0 if prtclbno == 1 & after == 0
replace current_winner = 0 if prtclbno == 2 & after == 0
replace current_winner = 0 if prtclbno == 3 & after == 0
replace current_winner = 0 if prtclbno == 6 & after == 0
replace current_winner = 0 if prtclbno == 8 & after == 0
replace current_winner = 0 if prtclbno == 9 & after == 0
replace current_winner = 0 if prtclbno == 10 & after == 0
replace current_winner = 0 if prtclbno == 11 & after == 0

// Save
save "$Data/ESS10, Norway 2.0.dta" , replace

********************************************************************************
* Sweden 2018
********************************************************************************

// Upload dataset
use "ESS9, Sweden.dta", clear

// -----------------------------------------------------------------------------
// Date Variables: Calculate the Cut-off Dates and Respondent Interview Dates
// -----------------------------------------------------------------------------

// Define the election day reference date in Stata's date format
gen ref_date_election = mdy(9, 13, 2018)

// Calculate the observation date in Stata's date format using "inwyys", "inwmms", and "inwdds"
gen obs_date = mdy(inwmms, inwdds, inwyys)

// Calculate the difference in days between the observation date and the reference date
gen days_election = obs_date - ref_date_election

// Create a dummy variable for whether the interview occurred before or after election day
gen after_election = 0
replace after_election = 1 if days_election>0

// Generate the reference date for the establishment of the election outcome in Stata's date format
gen ref_date = mdy(1, 18, 2019)

// Calculate days between interview date and the date when election results became clear 
gen days = obs_date - ref_date

// Create a dummy variable for observations made before or after the election results solidified
gen after = 0
replace after = 1 if days>0

// -----------------------------------------------------------------------------
// Manual classification of winners and losers
// -----------------------------------------------------------------------------

// Create dummy for electoral winners (1 = supported a party that entered government following the election, 0 = supported a party that remained in or moved to opposition)
gen winner =.

// Assign winner status based on vote in last election
replace winner = 1 if prtvtcse == 4 
replace winner = 1 if prtvtcse == 6 

replace winner = 0 if prtvtcse == 1 
replace winner = 0 if prtvtcse == 2 
replace winner = 0 if prtvtcse == 7 

replace winner = 0 if prtvtcse == 3
replace winner = 0 if prtvtcse == 5 
replace winner = 0 if prtvtcse == 8 
replace winner = 0 if prtvtcse == 9 
replace winner = 0 if prtvtcse == 10 

// If available, override with winner status based on party respondent currently feels closest to
replace winner = 1 if prtclcse == 4 
replace winner = 1 if prtclcse == 6 

replace winner = 0 if prtclcse == 1 
replace winner = 0 if prtclcse == 2 
replace winner = 0 if prtclcse == 7 

replace winner = 0 if prtclcse == 3
replace winner = 0 if prtclcse == 5 
replace winner = 0 if prtclcse == 8 
replace winner = 0 if prtclcse == 9 
replace winner = 0 if prtclcse == 10 

// Assign winner status at time of the interview (1 = winner, 0 = loser)
gen current_winner =.

replace current_winner = 1 if prtclcse == 4 
replace current_winner = 1 if prtclcse == 6 

replace current_winner = 0 if prtclcse == 1 
replace current_winner = 0 if prtclcse == 2 
replace current_winner = 0 if prtclcse == 7 

replace current_winner = 0 if prtclcse == 3
replace current_winner = 0 if prtclcse == 5 
replace current_winner = 0 if prtclcse == 8 
replace current_winner = 0 if prtclcse == 9 
replace current_winner = 0 if prtclcse == 10 

// Save
save "$Data/ESS9, Sweden 2.0.dta" , replace

********************************************************************************
* Ireland 2007
********************************************************************************

// Upload dataset
use "ESS3, Ireland.dta", clear

// -----------------------------------------------------------------------------
// Date Variables: Calculate the Cut-off Dates and Respondent Interview Dates
// -----------------------------------------------------------------------------

// Define the election day reference date in Stata's date format
gen ref_date_election = mdy(5, 24, 2007)

// Calculate the observation date in Stata's date format using "inwyys", "inwmms", and "inwdds"
gen obs_date = mdy(inwmms, inwdds, inwyys)

// Calculate the difference in days between the observation date and the reference date
gen days_election = obs_date - ref_date_election

// Create a dummy variable for whether the interview occurred before or after election day
gen after_election = 0
replace after_election = 1 if days_election>0

// Generate the reference date for the establishment of the election outcome in Stata's date format
gen ref_date = mdy(6, 12, 2007)

// Calculate days between interview date and the date when election results became clear 
gen days = obs_date - ref_date

// Create a dummy variable for observations made before or after the election results solidified
gen after = 0
replace after = 1 if days>0

// -----------------------------------------------------------------------------
// Manual classification of winners and losers
// -----------------------------------------------------------------------------

// Create dummy for electoral winners (1 = supported a party that entered government following the election, 0 = supported a party that remained in or moved to opposition)
gen winner =.

// Assign winner status based on vote in last election
replace winner = 1 if prtvtie == 1 
replace winner = 1 if prtvtie == 4
replace winner = 1 if prtvtie == 5 

replace winner = 0 if prtvtie == 2
replace winner = 0 if prtvtie == 3
replace winner = 0 if prtvtie == 6
replace winner = 0 if prtvtie == 7
replace winner = 0 if prtvtie == 8

// If available, override with winner status based on party respondent currently feels closest to
replace winner = 1 if prtclaie == 1 
replace winner = 1 if prtclaie == 4
replace winner = 1 if prtclaie == 5 

replace winner = 0 if prtclaie == 2
replace winner = 0 if prtclaie == 3
replace winner = 0 if prtclaie == 6
replace winner = 0 if prtclaie == 7
replace winner = 0 if prtclaie == 8

// Assign winner status at time of the interview (1 = winner, 0 = loser)
gen current_winner =.
replace current_winner = 1 if prtclaie == 1 & after==0
replace current_winner = 1 if prtclaie == 4 & after==0

replace current_winner = 0 if prtclaie == 2 & after==0
replace current_winner = 0 if prtclaie == 3 & after==0
replace current_winner = 0 if prtclaie == 6 & after==0
replace current_winner = 0 if prtclaie == 7 & after==0
replace current_winner = 0 if prtclaie == 8 & after==0

replace current_winner = 1 if prtclaie == 1 & after==1
replace current_winner = 1 if prtclaie == 5 & after==1

replace current_winner = 0 if prtclaie == 4 & after==1
replace current_winner = 0 if prtclaie == 2 & after==1
replace current_winner = 0 if prtclaie == 3 & after==1
replace current_winner = 0 if prtclaie == 6 & after==1
replace current_winner = 0 if prtclaie == 7 & after==1
replace current_winner = 0 if prtclaie == 8 & after==1

// Save
save "$Data/ESS3, Ireland 2.0.dta" , replace

********************************************************************************
* Netherlands 2012
********************************************************************************

// Upload dataset
use "ESS6, Netherlands.dta", clear

// -----------------------------------------------------------------------------
// Date Variables: Calculate the Cut-off Dates and Respondent Interview Dates
// -----------------------------------------------------------------------------

// Define the election day reference date in Stata's date format
gen ref_date_election = mdy(9, 12, 2012)

// Calculate the observation date in Stata's date format using "inwyys", "inwmms", and "inwdds"
gen obs_date = mdy(inwmms, inwdds, inwyys)

// Calculate the difference in days between the observation date and the reference date
gen days_election = obs_date - ref_date_election

// Create a dummy variable for whether the interview occurred before or after election day
gen after_election = 0
replace after_election = 1 if days_election>0

// Generate the reference date for the establishment of the election outcome in Stata's date format
gen ref_date = mdy(11, 5, 2012)

// Calculate days between interview date and the date when election results became clear 
gen days = obs_date - ref_date

// Create a dummy variable for observations made before or after the election results solidified
gen after = 0
replace after = 1 if days>0

// -----------------------------------------------------------------------------
// Manual classification of winners and losers
// -----------------------------------------------------------------------------

// Create dummy for electoral winners (1 = supported a party that entered government following the election, 0 = supported a party that remained in or moved to opposition)
gen winner =.

// Assign winner status based on vote in last election
replace winner = 1 if prtvtenl == 1
replace winner = 1 if prtvtenl == 2 
replace winner = 0 if prtvtenl == 3 
replace winner = 0 if prtvtenl == 4 
replace winner = 0 if prtvtenl == 5 
replace winner = 0 if prtvtenl == 6 
replace winner = 0 if prtvtenl == 7 
replace winner = 0 if prtvtenl == 8 
replace winner = 0 if prtvtenl == 9 
replace winner = 0 if prtvtenl == 10 
replace winner = 0 if prtvtenl == 11 
replace winner = 0 if prtvtenl == 12 
replace winner = 0 if prtvtenl == 13 

// If available, override with winner status based on party respondent currently feels closest to
replace winner = 1 if prtcldnl == 1
replace winner = 1 if prtcldnl == 2 
replace winner = 0 if prtcldnl == 3 
replace winner = 0 if prtcldnl == 4 
replace winner = 0 if prtcldnl == 5 
replace winner = 0 if prtcldnl == 6 
replace winner = 0 if prtcldnl == 7 
replace winner = 0 if prtcldnl == 8 
replace winner = 0 if prtcldnl == 9 
replace winner = 0 if prtcldnl == 10 
replace winner = 0 if prtcldnl == 11 
replace winner = 0 if prtcldnl == 12 
replace winner = 0 if prtcldnl == 13 

// Assign winner status at time of the interview (1 = winner, 0 = loser)
gen current_winner =.

replace current_winner = 1 if prtcldnl == 1 & after==1
replace current_winner = 1 if prtcldnl == 2 & after==1
replace current_winner = 0 if prtcldnl == 3 & after==1
replace current_winner = 0 if prtcldnl == 4 & after==1
replace current_winner = 0 if prtcldnl == 5 & after==1
replace current_winner = 0 if prtcldnl == 6 & after==1
replace current_winner = 0 if prtcldnl == 7 & after==1
replace current_winner = 0 if prtcldnl == 8 & after==1
replace current_winner = 0 if prtcldnl == 9 & after==1
replace current_winner = 0 if prtcldnl == 10 & after==1
replace current_winner = 0 if prtcldnl == 11 & after==1
replace current_winner = 0 if prtcldnl == 12 & after==1
replace current_winner = 0 if prtcldnl == 13 & after==1

replace current_winner = 1 if prtcldnl == 1 & after==0
replace current_winner = 1 if prtcldnl == 4 & after==0
replace current_winner = 1 if prtcldnl == 8 & after==0
replace current_winner = 0 if prtcldnl == 2 & after==0
replace current_winner = 0 if prtcldnl == 3 & after==0
replace current_winner = 0 if prtcldnl == 5 & after==0
replace current_winner = 0 if prtcldnl == 6 & after==0
replace current_winner = 0 if prtcldnl == 7 & after==0
replace current_winner = 0 if prtcldnl == 9 & after==0
replace current_winner = 0 if prtcldnl == 10 & after==0
replace current_winner = 0 if prtcldnl == 11 & after==0
replace current_winner = 0 if prtcldnl == 12 & after==0
replace current_winner = 0 if prtcldnl == 13 & after==0

// Save:
save "$Data/ESS6, Netherlands 2.0.dta" , replace

********************************************************************************
* Netherlands 2006
********************************************************************************

// Upload dataset 
use "ESS3, Netherlands.dta", clear

// -----------------------------------------------------------------------------
// Date Variables: Calculate the Cut-off Dates and Respondent Interview Dates
// -----------------------------------------------------------------------------

// Define the election day reference date in Stata's date format
gen ref_date_election = mdy(11, 22, 2006)

// Calculate the observation date in Stata's date format using "inwyys", "inwmms", and "inwdds"
gen obs_date = mdy(inwmms, inwdds, inwyys)

// Calculate the difference in days between the observation date and the reference date
gen days_election = obs_date - ref_date_election

// Create a dummy variable for whether the interview occurred before or after election day
gen after_election = 0
replace after_election = 1 if days_election>0

// Generate the reference date for the establishment of the election outcome in Stata's date format
gen ref_date = mdy(12, 17, 2006)

// Calculate days between interview date and the date when election results became clear 
gen days = obs_date - ref_date

// Create a dummy variable for observations made before or after the election results solidified
gen after = 0
replace after = 1 if days>0

// -----------------------------------------------------------------------------
// Manual classification of winners and losers
// -----------------------------------------------------------------------------

// Create dummy for electoral winners (1 = supported a party that entered government following the election, 0 = supported a party that remained in or moved to opposition)
gen winner =.

// Assign winner status based on vote in last election
replace winner = 1 if prtvtbnl == 1 
replace winner = 1 if prtvtbnl == 8
replace winner = 1 if prtvtbnl == 2

replace winner = 0 if prtvtbnl == 3
replace winner = 0 if prtvtbnl == 4
replace winner = 0 if prtvtbnl == 5
replace winner = 0 if prtvtbnl == 6
replace winner = 0 if prtvtbnl == 7
replace winner = 0 if prtvtbnl == 9
replace winner = 0 if prtvtbnl == 10
replace winner = 0 if prtvtbnl == 11

// If available, override with winner status based on party respondent currently feels closest to
replace winner = 1 if prtclnl == 1 
replace winner = 1 if prtclnl == 8
replace winner = 1 if prtclnl == 2

replace winner = 0 if prtclnl == 3
replace winner = 0 if prtclnl == 4
replace winner = 0 if prtclnl == 5
replace winner = 0 if prtclnl == 6
replace winner = 0 if prtclnl == 7
replace winner = 0 if prtclnl == 9
replace winner = 0 if prtclnl == 10
replace winner = 0 if prtclnl == 11

// Assign winner status at time of the interview (1 = winner, 0 = loser)
gen current_winner =.
replace current_winner = 1 if prtclnl == 1 & after==0
replace current_winner = 1 if prtclnl == 3 & after==0
replace current_winner = 1 if prtclnl == 5 & after==0

replace current_winner = 0 if prtclnl == 8 & after==0
replace current_winner = 0 if prtclnl == 2 & after==0
replace current_winner = 0 if prtclnl == 4 & after==0
replace current_winner = 0 if prtclnl == 6 & after==0
replace current_winner = 0 if prtclnl == 7 & after==0
replace current_winner = 0 if prtclnl == 9 & after==0
replace current_winner = 0 if prtclnl == 10 & after==0
replace current_winner = 0 if prtclnl == 11 & after==0

replace current_winner = 1 if prtclnl == 1 & after==1
replace current_winner = 1 if prtclnl == 8 & after==1
replace current_winner = 1 if prtclnl == 2 & after==1

replace current_winner = 0 if prtclnl == 3 & after==1
replace current_winner = 0 if prtclnl == 4 & after==1
replace current_winner = 0 if prtclnl == 5 & after==1
replace current_winner = 0 if prtclnl == 6 & after==1
replace current_winner = 0 if prtclnl == 7 & after==1
replace current_winner = 0 if prtclnl == 9 & after==1
replace current_winner = 0 if prtclnl == 10 & after==1
replace current_winner = 0 if prtclnl == 11 & after==1

*save:
save "$Data/ESS3, Netherlands 2.0.dta" , replace

********************************************************************************
*Append all data into one dataset
********************************************************************************

use "$Data/ESS3, Netherlands 2.0.dta", clear
append using "$Data/ESS6, Netherlands 2.0.dta", force
append using "$Data/ESS3, Ireland 2.0.dta", force
append using "$Data/ESS9, Sweden 2.0.dta", force
append using "$Data/ESS10, Norway 2.0.dta", force
append using "$Data/ESS10, Iceland 2.0.dta", force
append using "$Data/ESS8, Austria 2.0.dta", force
append using "$Data/ESS7, Poland 2.0.dta", force
append using "$Data/ESS4, Greece 2.0.dta", force
append using "$Data/ESS2, Italy 2.0.dta", force

append using "$Data/ESS2, Portugal 2.0.dta", force
append using "$Data/ESS7, Sweden 2.0.dta", force 
append using "$Data/ESS10, Portugal 2.0.dta", force
append using "$Data/ESS3, Estonia 2.0.dta", force 
append using "$Data/ESS5, Croatia 2.0.dta", force 
append using "$Data/ESS7, Portugal 2.0.dta", force 
append using "$Data/ESS9, Spain 2.0.dta", force
append using "$Data/ESS6, Slovenia 2.0.dta", force 
append using "$Data/ESS9, Croatia 2.0.dta", force
append using "$Data/ESS10, Serbia 2.0.dta", force
append using "$Data/ESS5, Cyprus 2.0.dta", force 

********************************************************************************
* Apply Labels
********************************************************************************

label define winner 0 "Losers" 1 "Winners" 
label values winner winner 

label define current_winner 0 "L" 1 "W" 
label values current_winner current_winner 

label define after 0 "Before" 1 "After" 
label values after after 

label variable after     "After"
label variable agea      "Age"
label variable ctzcntr   "Citizenship"
label variable days      "Days"
label variable eduyrs    "Education"
label variable gndr      "Gender"
label variable hincfel   "Household Economy"
label variable hswrk     "Housework"
label variable lrscale   "Left-Right"
label variable rdpol     "Listen to News on Radio"
label variable nwsptot   "Newspaper Reading"
label variable pdwrk     "Paid Work"
label variable polintr   "Political Interest"
label variable rtrd      "Retired"
label variable stfdem    "SWD"
label variable stfeco    "Satisfaction with Economy"
label variable edctn     "Student"
label variable trstprl   "Trust in Parliament"
label variable trstprt   "Trust in Parties"
label variable trstplc   "Trust in Police"
label variable tstpboh   "Trust in Public Officials"
label variable uempla    "Unemployed"
label variable tvpol     "Watch News on TV"
label variable winner    "Winner"
label variable current_winner "Winner"

********************************************************************************
* Create dummy variables for country
********************************************************************************

generate country = .
replace country = 1 if cntry =="IT"
replace country = 2 if cntry =="IE"
replace country = 3 if cntry =="NL"
replace country = 4 if cntry =="SE"
replace country = 5 if cntry =="NO"
replace country = 6 if cntry =="IS"
replace country = 7 if cntry =="AT"
replace country = 8 if cntry =="GR"
replace country = 9 if cntry =="PL"
replace country = 10 if cntry =="IL"
replace country = 11 if cntry =="PT"
replace country = 12 if cntry =="EE"
replace country = 13 if cntry =="SI"
replace country = 14 if cntry =="HR"
replace country = 15 if cntry =="ES"
replace country = 16 if cntry =="RS"
replace country = 17 if cntry =="CY"

tab country, gen(country)

********************************************************************************
* Generate logged and squared dependent variables
********************************************************************************

gen stfdem_log=log(stfdem)
gen stfgov_log=log(stfgov)
gen trstprl_log=log(trstprl)
gen stfdem_squared=stfdem^2

********************************************************************************
* Generate dummy denoting if a government transition occured (1) or not (0)
********************************************************************************

gen transition =.
replace transition = 0 if cntry=="IS" 
replace transition = 0 if cntry=="IE" 
replace transition = 0 if cntry=="SE"
replace transition = 1 if cntry=="IT" 
replace transition = 1 if cntry=="NO" 
replace transition = 1 if cntry=="GR" 
replace transition = 1 if cntry=="PL" 
replace transition = 1 if cntry=="NL" 
replace transition = 0 if cntry=="AT" 
replace transition = 0 if cntry=="IL" 
replace transition = 1 if cntry=="PT" & essround==2
replace transition = 1 if cntry=="SE" & essround==7
replace transition = 0 if cntry=="PT" & essround==10
replace transition = 0 if cntry=="EE" & essround==3 
replace transition = 1 if cntry=="SI" & essround==4 
replace transition = 1 if cntry=="HR" & essround==5 
replace transition = 1 if cntry=="PT" & essround==7 
replace transition = 0 if cntry=="ES" & essround==9 
replace transition = 1 if cntry=="SI" & essround==6 
replace transition = 1 if cntry=="HR" & essround==9
replace transition = 0 if cntry=="RS" & essround==10
replace transition = 0 if cntry=="CY" & essround==5
 
********************************************************************************
* Group observations by week relative to the reference date
********************************************************************************

gen week = floor(days / 7)

label variable week "Week"

********************************************************************************
* Generate Weekly and Daily Averages of Satisfaction with Democracy (stfdem)
********************************************************************************

// Calculate average stfdem by week for each country, round, and winner status
sort country essround week
bysort week country essround winner: egen week_stfdem=mean(stfdem)

// Create lagged weekly stfdem variable
gen lag_week_stfdem = .
by country (essround week winner), sort: replace lag_week_stfdem = week_stfdem[_n-1]

// Calculate average stfdem by day for each country, round, and winner status
sort country essround days
bysort days country essround winner: egen days_stfdem=mean(stfdem)

// Create lagged daily stfdem variable
gen lag_days_stfdem = .
by country (essround days winner), sort: replace lag_days_stfdem = days_stfdem[_n-1]

*Save dataset
save "$Data/Results.dta" , replace

********************************************************************************
* Clean and Process ESS Metadata (Rounds 2–10)
********************************************************************************

// -----------------------------------------------------------------------------
// ESS Round 2
// -----------------------------------------------------------------------------

use "ESS2CFe03_2.dta", clear

// Calculate total number of visits
egen total_visits = rowtotal(totcin*)

// Count number of refusals to participate
forval i = 1/10 {
    gen refusal`i' = (outnia`i' == 2 | outnia`i' == 3 | outnia`i' == 4)
}

egen total_refusals = rowtotal(refusal*)

// Keep relevant countries only
keep if cntry=="IT" | cntry=="PT"

// Save 
save "$Data/ESS2_done.dta", replace

// -----------------------------------------------------------------------------
// ESS Round 3
// -----------------------------------------------------------------------------

use "ESS3CF_ed1_1.dta", clear

// Calculate total number of visits
egen total_visits = rowtotal(totcin*)

// Count number of refusals to participate
forval i = 1/93 {
    gen refusal`i' = (outnia`i' == 2 | outnia`i' == 3 | outnia`i' == 4)
}

egen total_refusals = rowtotal(refusal*)

// Keep relevant countries only
keep if cntry=="NL" | cntry=="IE" | cntry=="EE"

// Save 
save "$Data/ESS3_done.dta", replace

// -----------------------------------------------------------------------------
// ESS Round 4
// -----------------------------------------------------------------------------

use "ESS4CF_e02_1.dta", clear

// Calculate total number of visits
egen total_visits = rowtotal(totcin*)

// Count number of refusals to participate
forval i = 1/59 {
    gen refusal`i' = (outnib`i' == 2 | outnib`i' == 3 | outnib`i' == 4)
}

egen total_refusals = rowtotal(refusal*)

// Keep relevant countries only
keep if cntry=="GR" 

//Save 
save "$Data/ESS4_done.dta", replace

// -----------------------------------------------------------------------------
// ESS Round 5
// -----------------------------------------------------------------------------

use "ESS5CFe02_1.dta", clear

// Calculate total number of visits
egen total_visits = rowtotal(totcin*)

// Count number of refusals to participate
forval i = 1/86 {
    gen refusal`i' = (outnib`i' == 2 | outnib`i' == 3 | outnib`i' == 4)
}

egen total_refusals = rowtotal(refusal*)

// Keep relevant countries only
keep if cntry=="HR" | cntry=="CY" 

// Save 
save "$Data/ESS5_done.dta", replace

// -----------------------------------------------------------------------------
// ESS Round 6
// -----------------------------------------------------------------------------

use "ESS6CFe02.dta", clear

// Calculate total number of visits
egen total_visits = rowtotal(totcin*)

// Count number of refusals to participate
forval i = 1/69 {
    gen refusal`i' = (outnic`i' == 2 | outnic`i' == 3 | outnic`i' == 4)
}

egen total_refusals = rowtotal(refusal*)

// Keep relevant countries only
keep if cntry=="NL" | cntry=="SI" 

// Save 
save "$Data/ESS6_done.dta", replace

// -----------------------------------------------------------------------------
// ESS Round 7
// -----------------------------------------------------------------------------

use "ESS7CFe02_1.dta", clear

// Calculate total number of visits
egen total_visits = rowtotal(totcin*)

// Count number of refusals to participate
forval i = 1/80 {
    gen refusal`i' = (outnic`i' == 2 | outnic`i' == 3 | outnic`i' == 4)
}

egen total_refusals = rowtotal(refusal*)

// Keep relevant countries only
keep if cntry=="PL" | cntry=="SE" | cntry=="PT" 

// Save 
save "$Data/ESS7_done.dta", replace

// -----------------------------------------------------------------------------
// ESS Round 8
// -----------------------------------------------------------------------------

use "ESS8CFe03.dta", clear

// Calculate total number of visits
egen total_visits = rowtotal(totcin*)

// Count number of refusals to participate
forval i = 1/67 {
    gen refusal`i' = (outnic`i' == 2 | outnic`i' == 3 | outnic`i' == 4)
}

egen total_refusals = rowtotal(refusal*)

// Keep relevant countries only
keep if cntry=="AT" 

// Save 
save "$Data/ESS8_done.dta", replace

// -----------------------------------------------------------------------------
// ESS Round 9
// -----------------------------------------------------------------------------

use "ESS9CFe03.dta", clear

// Calculate total number of visits
egen total_visits = rowtotal(totcin*)

// Count number of refusals to participate
forval i = 1/89 {
    gen refusal`i' = (outnic`i' == 2 | outnic`i' == 3 | outnic`i' == 4)
}

egen total_refusals = rowtotal(refusal*)

// Keep relevant countries only
keep if cntry=="SE" | cntry=="ES" | cntry=="HR" 

// Save 
save "$Data/ESS9_done.dta", replace

// -----------------------------------------------------------------------------
// ESS Round 10
// -----------------------------------------------------------------------------

use "ESS10CF.dta", clear

// Calculate total number of visits
egen total_visits = rowtotal(totcin*)

// Count number of refusals to participate
forval i = 1/37 {
    gen refusal`i' = (outnic`i' == 2 | outnic`i' == 3 | outnic`i' == 4)
}

egen total_refusals = rowtotal(refusal*)

// Keep relevant countries only
keep if cntry=="NO" | cntry=="IS" | cntry=="PT" | cntry=="RS" 

// Save 
save "$Data/ESS10_done.dta", replace

// -----------------------------------------------------------------------------
// Append All Rounds into Single Metadata File
// -----------------------------------------------------------------------------

use "$Data/ESS2_done.dta", clear
append using "$Data/ESS3_done.dta"
append using "$Data/ESS4_done.dta"
append using "$Data/ESS5_done.dta"
append using "$Data/ESS6_done.dta"
append using "$Data/ESS7_done.dta"
append using "$Data/ESS8_done.dta"
append using "$Data/ESS9_done.dta"
append using "$Data/ESS10_done.dta"

save "$Data/ESS_meta.dta", replace

// -----------------------------------------------------------------------------
// Merge Metadata with main Results dataset
// -----------------------------------------------------------------------------

use "$Data/Results.dta", clear
merge m:m cntry essround idno using "$Data/ESS_meta.dta", force

********************************************************************************
* Create lagged daily averages for total visits and total refusals before survey completion
********************************************************************************

// Lagged total_visits
sort country essround days
bysort days country essround winner: egen days_visits=mean(total_visits)

gen lag_days_visits = .
by country (essround days winner), sort: replace lag_days_visits = days_visits[_n-1]

// Lagged total_refusals
sort country essround days
bysort days country essround winner: egen days_refusals=mean(total_refusals)

gen lag_days_refusals = .
by country (essround days winner), sort: replace lag_days_refusals = days_refusals[_n-1]

********************************************************************************
* Generate cluster ID and save final dataset
********************************************************************************

egen cluster_id = group(essround country)
save "$Data/Results+meta.dta", replace

********************************************************************************
* Figure 1: Satisfaction with democracy among 
*           losers (L) and winners (W), by country
********************************************************************************

graph bar stfdem, ///
    over(current_winner) ///
    over(cntry) ///
    ytitle(Satisfaction with Democracy)

// Save the Graph in the Figures Folder
graph export "$Figures/Figure 1.pdf", replace

********************************************************************************
* Figure 2: Histogram of distance (days) from election
********************************************************************************

* Visualize distribution of observations around election day
histogram days if days > -100 & days < 100, ///
    xline(0) ///
    xtitle(Days) ///
    bin(30)

// Save the Graph in the Figures Folder
graph export "$Figures/Figure 2.pdf" , replace

********************************************************************************
* Figure 3: Balance Tests (Panels A–H)
********************************************************************************

// Step 1: Rescale variables with large values for interpretability
gen agea_10=agea/10
gen eduyrs_10=eduyrs/10
gen total_visits_10=total_visits/10

// ---------------------------------------------------------------------
// Panel A
// ---------------------------------------------------------------------

// Define parameters for Losers, 100 days bandwidth:
local variables agea_10 eduyrs_10 ctzcntr gndr pdwrk uempla edctn rtrd hswrk lrscale polintr total_visits_10 total_refusals //Variables to analyze
local winner_status 0  // Set winner status
local bandwidth 100     // Set bandwidth ///

// Loop through variables to run rdrobust and store results
foreach var in `variables' {
    rdrobust `var' days if winner==`winner_status', ///
        covs(country* essround) p(1) masspoints(off) h(`bandwidth' `bandwidth') kernel(epanechnikov)
    estimates store `var'_`bandwidth'  // Store results with unique name
}

// Plot results
coefplot (agea_10_`bandwidth', aseq(Age (N/10)) ///
        \ eduyrs_10_`bandwidth', aseq(Education (N/10)) ///
		\ ctzcntr_`bandwidth', aseq(Citizenship) ///
		\ gndr_`bandwidth', aseq(Gender) ///
		\ pdwrk_`bandwidth', aseq(Paid work) ///
		\ uempla_`bandwidth', aseq(Unemployed) ///
		\ rtrd_`bandwidth', aseq(Retired) ///
		\ edctn_`bandwidth', aseq(Student) ///
		\ hswrk_`bandwidth', aseq(Housework) ///
		\ lrscale_`bandwidth', aseq(Left–right) ///
		\ polintr_`bandwidth', aseq(Political interest) ///
		\ total_visits_10_`bandwidth', aseq(Attempts to survey (N/10)) ///
		\ total_refusals_`bandwidth', aseq("Refusals to participate")) ///
       , drop(_cons) swapnames xline(0) yscale(lstyle(none))  title("`bandwidth'±")  

//Save individual panel:
graph save Figure_0_100.gph, replace

// ---------------------------------------------------------------------
// Panel B
// ---------------------------------------------------------------------

// Define parameters for Losers, 30 days bandwidth:
local variables agea_10 eduyrs_10 ctzcntr gndr pdwrk uempla edctn rtrd hswrk lrscale polintr total_visits_10 total_refusals //Variables to analyze
local winner_status 0  // Set winner status
local bandwidth 30     // Set bandwidth

// Loop through variables to run rdrobust and store results
foreach var in `variables' {
    rdrobust `var' days if winner==`winner_status', ///
        covs(country* essround) p(1) masspoints(off) h(`bandwidth' `bandwidth') kernel(epanechnikov)
    estimates store `var'_`bandwidth'  // Store results with unique name
}

// Plot results
coefplot (agea_10_`bandwidth', aseq(Age (N/10)) ///
        \ eduyrs_10_`bandwidth', aseq(Education (N/10)) ///
		\ ctzcntr_`bandwidth', aseq(Citizenship) ///
		\ gndr_`bandwidth', aseq(Gender) ///
		\ pdwrk_`bandwidth', aseq(Paid work) ///
		\ uempla_`bandwidth', aseq(Unemployed) ///
		\ rtrd_`bandwidth', aseq(Retired) ///
		\ edctn_`bandwidth', aseq(Student) ///
		\ hswrk_`bandwidth', aseq(Housework) ///
		\ lrscale_`bandwidth', aseq(Left–right) ///
		\ polintr_`bandwidth', aseq(Political interest) ///
		\ total_visits_10_`bandwidth', aseq(Attempts to survey (N/10)) ///
		\ total_refusals_`bandwidth', aseq("Refusals to participate")) ///
       , drop(_cons) swapnames xline(0) yscale(lstyle(none))  title("`bandwidth'±") ///  

//Save individual panel:
graph save Figure_0_30.gph, replace

// ---------------------------------------------------------------------
// Panel C
// ---------------------------------------------------------------------

// Define parameters for Losers, 20 days bandwidth:
local variables agea_10 eduyrs_10 ctzcntr gndr pdwrk uempla edctn rtrd hswrk lrscale polintr total_visits_10 total_refusals //Variables to analyze
local winner_status 0  // Set winner status
local bandwidth 20     // Set bandwidth

// Loop through variables to run rdrobust and store results
foreach var in `variables' {
    rdrobust `var' days if winner==`winner_status', ///
        covs(country* essround) p(1) masspoints(off) h(`bandwidth' `bandwidth') kernel(epanechnikov)
    estimates store `var'_`bandwidth'  // Store results with unique name
}

// Plot results
coefplot (agea_10_`bandwidth', aseq(Age (N/10)) ///
        \ eduyrs_10_`bandwidth', aseq(Education (N/10)) ///
		\ ctzcntr_`bandwidth', aseq(Citizenship) ///
		\ gndr_`bandwidth', aseq(Gender) ///
		\ pdwrk_`bandwidth', aseq(Paid work) ///
		\ uempla_`bandwidth', aseq(Unemployed) ///
		\ rtrd_`bandwidth', aseq(Retired) ///
		\ edctn_`bandwidth', aseq(Student) ///
		\ hswrk_`bandwidth', aseq(Housework) ///
		\ lrscale_`bandwidth', aseq(Left–right) ///
		\ polintr_`bandwidth', aseq(Political interest) ///
		\ total_visits_10_`bandwidth', aseq(Attempts to survey (N/10)) ///
		\ total_refusals_`bandwidth', aseq("Refusals to participate")) ///
       , drop(_cons) swapnames xline(0) yscale(lstyle(none))  title("`bandwidth'±") ///  

//Save individual panel:
graph save Figure_0_20.gph, replace

// ---------------------------------------------------------------------
// Panel D
// ---------------------------------------------------------------------

// Define parameters for Losers, 16 days bandwidth:
local variables agea_10 eduyrs_10 ctzcntr gndr pdwrk uempla edctn rtrd hswrk lrscale polintr total_visits_10 total_refusals //Variables to analyze
local winner_status 0  // Set winner status
local bandwidth 16     // Set bandwidth

// Loop through variables to run rdrobust and store results
foreach var in `variables' {
    rdrobust `var' days if winner==`winner_status', ///
        covs(country* essround) p(1) masspoints(off) h(`bandwidth' `bandwidth') kernel(epanechnikov)
    estimates store `var'_`bandwidth'  // Store results with unique name
}

// Plot results
coefplot (agea_10_`bandwidth', aseq(Age (N/10)) ///
        \ eduyrs_10_`bandwidth', aseq(Education (N/10)) ///
		\ ctzcntr_`bandwidth', aseq(Citizenship) ///
		\ gndr_`bandwidth', aseq(Gender) ///
		\ pdwrk_`bandwidth', aseq(Paid work) ///
		\ uempla_`bandwidth', aseq(Unemployed) ///
		\ rtrd_`bandwidth', aseq(Retired) ///
		\ edctn_`bandwidth', aseq(Student) ///
		\ hswrk_`bandwidth', aseq(Housework) ///
		\ lrscale_`bandwidth', aseq(Left–right) ///
		\ polintr_`bandwidth', aseq(Political interest) ///
		\ total_visits_10_`bandwidth', aseq(Attempts to survey (N/10)) ///
		\ total_refusals_`bandwidth', aseq("Refusals to participate")) ///
       , drop(_cons) swapnames xline(0) yscale(lstyle(none))  title("`bandwidth'±") ///  

//Save individual panel:
graph save Figure_0_16.gph, replace

//Combine panel A–D
graph combine Figure_0_100.gph Figure_0_30.gph Figure_0_20.gph Figure_0_16.gph,  saving(Figure_Balance_tests_losers.gph, replace)  cols(2) ysize(9) xsize(15) title("Losers") imargin(0 0 0 0) 

// ---------------------------------------------------------------------
// Panel E
// ---------------------------------------------------------------------

// Define parameters for winners, 100 days bandwidth:
local variables agea_10 eduyrs_10 ctzcntr gndr pdwrk uempla edctn rtrd hswrk lrscale polintr total_visits_10 total_refusals //Variables to analyze
local winner_status 1  // Set winner status
local bandwidth 100     // Set bandwidth

// Loop through variables to run rdrobust and store results
foreach var in `variables' {
    rdrobust `var' days if winner==`winner_status', ///
        covs(country* essround) p(1) masspoints(off) h(`bandwidth' `bandwidth') kernel(epanechnikov)
    estimates store `var'_`bandwidth'  // Store results with unique name
}

// Plot results
coefplot (agea_10_`bandwidth', aseq(Age (N/10)) ///
        \ eduyrs_10_`bandwidth', aseq(Education (N/10)) ///
		\ ctzcntr_`bandwidth', aseq(Citizenship) ///
		\ gndr_`bandwidth', aseq(Gender) ///
		\ pdwrk_`bandwidth', aseq(Paid work) ///
		\ uempla_`bandwidth', aseq(Unemployed) ///
		\ rtrd_`bandwidth', aseq(Retired) ///
		\ edctn_`bandwidth', aseq(Student) ///
		\ hswrk_`bandwidth', aseq(Housework) ///
		\ lrscale_`bandwidth', aseq(Left–right) ///
		\ polintr_`bandwidth', aseq(Political interest) ///
		\ total_visits_10_`bandwidth', aseq(Attempts to survey (N/10)) ///
		\ total_refusals_`bandwidth', aseq("Refusals to participate")) ///
       , drop(_cons) swapnames xline(0) yscale(lstyle(none))  title("`bandwidth'±") ///  

//Save individual panel:
graph save Figure_1_100.gph, replace

// ---------------------------------------------------------------------
// Panel F
// ---------------------------------------------------------------------

// Define parameters for winners, 30 days bandwidth:
local variables agea_10 eduyrs_10 ctzcntr gndr pdwrk uempla edctn rtrd hswrk lrscale polintr total_visits_10 total_refusals //Variables to analyze
local winner_status 1  // Set winner status
local bandwidth 30     // Set bandwidth

// Loop through variables to run rdrobust and store results
foreach var in `variables' {
    rdrobust `var' days if winner==`winner_status', ///
        covs(country* essround) p(1) masspoints(off) h(`bandwidth' `bandwidth') kernel(epanechnikov)
    estimates store `var'_`bandwidth'  // Store results with unique name
}

// Plot results
coefplot (agea_10_`bandwidth', aseq(Age (N/10)) ///
        \ eduyrs_10_`bandwidth', aseq(Education (N/10)) ///
		\ ctzcntr_`bandwidth', aseq(Citizenship) ///
		\ gndr_`bandwidth', aseq(Gender) ///
		\ pdwrk_`bandwidth', aseq(Paid work) ///
		\ uempla_`bandwidth', aseq(Unemployed) ///
		\ rtrd_`bandwidth', aseq(Retired) ///
		\ edctn_`bandwidth', aseq(Student) ///
		\ hswrk_`bandwidth', aseq(Housework) ///
		\ lrscale_`bandwidth', aseq(Left–right) ///
		\ polintr_`bandwidth', aseq(Political interest) ///
		\ total_visits_10_`bandwidth', aseq(Attempts to survey (N/10)) ///
		\ total_refusals_`bandwidth', aseq("Refusals to participate")) ///
       , drop(_cons) swapnames xline(0) yscale(lstyle(none))  title("`bandwidth'±") ///  

//Save individual panel:
graph save Figure_1_30.gph, replace

// ---------------------------------------------------------------------
// Panel G
// ---------------------------------------------------------------------

// Define parameters for winners, 20 days bandwidth:
local variables agea_10 eduyrs_10 ctzcntr gndr pdwrk uempla edctn rtrd hswrk lrscale polintr total_visits_10 total_refusals //Variables to analyze
local winner_status 1  // Set winner status
local bandwidth 20     // Set bandwidth

// Loop through variables to run rdrobust and store results
foreach var in `variables' {
    rdrobust `var' days if winner==`winner_status', ///
        covs(country* essround) p(1) masspoints(off) h(`bandwidth' `bandwidth') kernel(epanechnikov)
    estimates store `var'_`bandwidth'  // Store results with unique name
}

// Plot results
coefplot (agea_10_`bandwidth', aseq(Age (N/10)) ///
        \ eduyrs_10_`bandwidth', aseq(Education (N/10)) ///
		\ ctzcntr_`bandwidth', aseq(Citizenship) ///
		\ gndr_`bandwidth', aseq(Gender) ///
		\ pdwrk_`bandwidth', aseq(Paid work) ///
		\ uempla_`bandwidth', aseq(Unemployed) ///
		\ rtrd_`bandwidth', aseq(Retired) ///
		\ edctn_`bandwidth', aseq(Student) ///
		\ hswrk_`bandwidth', aseq(Housework) ///
		\ lrscale_`bandwidth', aseq(Left–right) ///
		\ polintr_`bandwidth', aseq(Political interest) ///
		\ total_visits_10_`bandwidth', aseq(Attempts to survey (N/10)) ///
		\ total_refusals_`bandwidth', aseq("Refusals to participate")) ///
       , drop(_cons) swapnames xline(0) yscale(lstyle(none))  title("`bandwidth'±") ///  

//Save individual panel:
graph save Figure_1_20.gph, replace

// ---------------------------------------------------------------------
// Panel H
// ---------------------------------------------------------------------

// Define parameters for winners, 16 days bandwidth:
local variables agea_10 eduyrs_10 ctzcntr gndr pdwrk uempla edctn rtrd hswrk lrscale polintr total_visits_10 total_refusals //Variables to analyze
local winner_status 1  // Set winner status
local bandwidth 16     // Set bandwidth

// Loop through variables to run rdrobust and store results
foreach var in `variables' {
    rdrobust `var' days if winner==`winner_status', ///
        covs(country* essround) p(1) masspoints(off) h(`bandwidth' `bandwidth') kernel(epanechnikov)
    estimates store `var'_`bandwidth'  // Store results with unique name
}

// Plot results
coefplot (agea_10_`bandwidth', aseq(Age (N/10)) ///
        \ eduyrs_10_`bandwidth', aseq(Education (N/10)) ///
		\ ctzcntr_`bandwidth', aseq(Citizenship) ///
		\ gndr_`bandwidth', aseq(Gender) ///
		\ pdwrk_`bandwidth', aseq(Paid work) ///
		\ uempla_`bandwidth', aseq(Unemployed) ///
		\ rtrd_`bandwidth', aseq(Retired) ///
		\ edctn_`bandwidth', aseq(Student) ///
		\ hswrk_`bandwidth', aseq(Housework) ///
		\ lrscale_`bandwidth', aseq(Left–right) ///
		\ polintr_`bandwidth', aseq(Political interest) ///
		\ total_visits_10_`bandwidth', aseq(Attempts to survey (N/10)) ///
		\ total_refusals_`bandwidth', aseq("Refusals to participate")) ///
       , drop(_cons) swapnames xline(0) yscale(lstyle(none))  title("`bandwidth'±") ///  

//Save individual panel:
graph save Figure_1_16.gph, replace

//Combine panel E–H
graph combine Figure_1_100.gph Figure_1_30.gph Figure_1_20.gph Figure_1_16.gph,  saving(Figure_Balance_tests_winners.gph, replace)  cols(2) ysize(9) xsize(15) title("Winners") imargin(0 0 0 0) 

// ---------------------------------------------------------------------
//Combine all panels
// ---------------------------------------------------------------------

graph combine Figure_Balance_tests_losers.gph Figure_Balance_tests_winners.gph,  saving(Figure_Balance_tests_combined.gph, replace)   title("Balance Tests") rows(2) ysize(12) xsize(10)

// Save the Graph in the Figures Folder
graph export "$Figures/Figure 3.pdf" , replace

********************************************************************************
* Figure 4: Satisfaction with democracy among winners and losers before and after election outcomes were established (Panels A–D)
********************************************************************************

// ---------------------------------------------------------------------
// Panel A: Losers (±100 days)
// ---------------------------------------------------------------------

rdplot stfdem days if winner == 0 & inrange(days, -100, 100), ///
    covs(country* essround lag_days_stfdem) h(100 100) masspoints(off) ///
    p(1) kernel(epanechnikov) ///
    graph_options( ///
        ytitle("Satisfaction with democracy") ///
        title("A) Losers ±100 (N: 9626)") ///
        legend(off) ///
        yscale(range(0 10)) ///
        ylabel(0(2)10) ///
    )
graph save Figure_4a.gph, replace

// ---------------------------------------------------------------------
// Panel B: All Winners (±100 days)
// ---------------------------------------------------------------------

rdplot stfdem days if winner == 1 & inrange(days, -100, 100), ///
    covs(country* essround lag_days_stfdem) h(100 100) masspoints(off) ///
    p(1) kernel(epanechnikov) ///
    graph_options( ///
        title("B) Winners ±100 (N: 8291)") ///
        ytitle("") ///
        aspectratio(1) ///
        legend(off) ///
        yscale(range(0 10)) ///
        ylabel(0(2)10) ///
    )
graph save Figure_4b.gph, replace

// ---------------------------------------------------------------------
// Panel C: Politically Interested Winners (±100 days)
// ---------------------------------------------------------------------

rdplot stfdem days if winner == 1 & inrange(days, -100, 100) & polintr < 3, ///
    covs(country* essround lag_days_stfdem) h(100 100) masspoints(off) ///
    p(1) kernel(epanechnikov) ///
    graph_options( ///
        title("C) Politically Interested Winners ±100 (N: 4574)") ///
        ytitle("Satisfaction with democracy") ///
        aspectratio(1) ///
        legend(off) ///
        yscale(range(0 10)) ///
        ylabel(0(2)10) ///
    )
graph save Figure_4c.gph, replace

// ---------------------------------------------------------------------
// Panel D: Politically Interested Winners (±16 days)
// ---------------------------------------------------------------------

rdplot stfdem days if winner == 1 & inrange(days, -16, 16) & polintr < 3, ///
    covs(country* essround lag_days_stfdem) h(16 16) masspoints(off) ///
    p(1) kernel(epanechnikov) ///
    graph_options( ///
        title("D) Politically Interested Winners ±16 (N: 991)") ///
        ytitle("") ///
        aspectratio(1) ///
        legend(off) ///
        yscale(range(0 10)) ///
        ylabel(0(2)10) ///
    )
graph save Figure_4d.gph, replace

// ---------------------------------------------------------------------
// Combine Panels A–D
// ---------------------------------------------------------------------

graph combine Figure_4a.gph Figure_4b.gph Figure_4c.gph Figure_4d.gph, ///
    rows(2) ycommon ///
    title("") ///
    imargin(0 0 0 0) ///
    iscale(0.5) xsize(12) ysize(12) ///
    saving(Figure_4_a+b+c+d.gph, replace)

// Save the Graph in the Figures Folder
graph export "$Figures/Figure 4.pdf" , replace
	
********************************************************************************
* Figure 5: Long-Term Effects by Government Outcome (Panels A–B)
********************************************************************************

// ---------------------------------------------------------------------
// Panel A: Government Transition
// ---------------------------------------------------------------------

use "$Data/Results+meta.dta", clear

// Recode week variables to avoid negative values in factor variable interactions
replace week = week + 100

// Estimate interaction model (only cases of government transition)
reg stfdem i.week##i.winner agea eduyrs gndr lrscale polintr hincfel i.country i.essround ///
    if transition == 1, robust

// Marginal effects of winner status across weeks
margins, dydx(winner) at(week == (98(1)105))

// Plot margins
marginsplot, ///
    xline(100) ///
    xtitle("Week") ///
    ytitle("Satisfaction with Democracy") ///
    title("Government Transition") ///
    xlabel(98 "-2" 99 "-1" 100 "0" 101 "1" 102 "2" 103 "3" 104 "4" 105 "5") ///
    ylabel(-2(1)3) ///
    yline(0) ///
    yscale(range(-1.5 1.5))

// Save graph
graph save Figure_5a.gph, replace

// ---------------------------------------------------------------------
// Panel B: Incumbent Re-election
// ---------------------------------------------------------------------

// Estimate interaction model (only cases of incumbent re-election)
reg stfdem i.week##i.winner eduyrs agea gndr lrscale polintr hincfel i.country i.essround ///
    if transition == 0, robust

// Marginal effects
margins, dydx(winner) at(week == (98(1)105))

// Plot margins
marginsplot, ///
    xline(100) ///
    xtitle("Week") ///
    ytitle("") ///
    title("Incumbent Re-election") ///
    xlabel(98 "-2" 99 "-1" 100 "0" 101 "1" 102 "2" 103 "3" 104 "4" 105 "5") ///
    ylabel(-2(1)3) ///
    yline(0) ///
    yscale(range(-1 3))

// Save graph
graph save Figure_5b.gph, replace

// ---------------------------------------------------------------------
// Combine Panels A and B
// ---------------------------------------------------------------------

graph combine Figure_5a.gph Figure_5b.gph, ///
    ycommon xcommon ///
    saving(Figure_5a+5b.gph, replace)

// Save the Graph in the Figures Folder
graph export "$Figures/Figure 5.pdf" , replace

********************************************************************************
* Figure 6: Long-Term Effects by Voter Type (Panels A–D)
********************************************************************************

// -----------------------------------------------------------------------------
// Panel A: Ideologically Moderate Voters
// -----------------------------------------------------------------------------
use "$Data/Results+meta.dta", clear

// Estimate model for ideologically moderate voters
reg stfdem c.week##i.winner i.country i.inwyys ///
    if lrscale > 3 & lrscale < 7 & days > -1

// Marginal effects
margins, at(week == (0(1)12) winner == (0 1))

// Plot
marginsplot, ///
    xline(0) ///
    xtitle("Week") ///
    ytitle("Satisfaction with Democracy") ///
    title("Moderate Voters (N: 2086)") ///
    legend(position(1 3))

graph save Figure_6a.gph, replace

// -----------------------------------------------------------------------------
// Panel B: Ideologically Radical Voters
// -----------------------------------------------------------------------------

// Estimate model for ideologically radical voters
reg stfdem c.week##i.winner agea gndr hincfel eduyrs polintr i.country i.inwyys ///
    if (lrscale < 4 | lrscale > 6) & days > -1

// Marginal effects
margins, at(week == (0(1)12) winner == (0 1))

// Plot
marginsplot, ///
    xline(0) ///
    xtitle("Week") ///
    ytitle("") ///
    title("Radical Voters (N: 2935)") ///
    legend(position(1 3))

graph save Figure_6b.gph, replace

// -----------------------------------------------------------------------------
// Panel C: High News Consumption
// -----------------------------------------------------------------------------

// Create news consumption index (mean of TV and newspaper exposure)
gen news = .
replace news = (tvpol + nwsptot)/2 if tvpol != . & nwsptot != .

// Create binary indicator for high news consumption (threshold: > 3)
gen news_consumption = .
replace news_consumption = 1 if news > 3
replace news_consumption = 0 if news < 4

// Run regression for individuals with high news consumption only
reg stfdem c.week##i.winner i.country i.essround i.transition##c.week ///
    if news_consumption == 1 & days > -1

// Estimate marginal effects over weeks for winners and losers
margins, at(week == (0(1)12) winner == (0 1))

// Plot
marginsplot, ///
    xline(0) ///
    xtitle("Week") ///
    ytitle("Satisfaction with Democracy") ///
    title("High Mainstream News Cons. (N: 3540)") ///
    legend(position(1 3))

graph save Figure_6c.gph, replace

// -----------------------------------------------------------------------------
// Panel D: Low News Consumption
// -----------------------------------------------------------------------------

// Run regression for individuals with low political knowledge only
reg stfdem c.week##i.winner i.country i.essround i.transition##c.week ///
    if news_consumption == 0 & days > -1

// Estimate marginal effects over weeks for winners and losers
margins, at(week == (0(1)12) winner == (0 1))

// Plot
marginsplot, ///
    xline(0) ///
    xtitle("Week") ///
    ytitle("") ///
    title("Low Mainstream News Cons. (N: 1481)") ///
    legend(position(1 3))

graph save Figure_6d.gph, replace

// -----------------------------------------------------------------------------
// Combine all panels into one figure
// -----------------------------------------------------------------------------
graph combine Figure_6a.gph Figure_6b.gph Figure_6c.gph Figure_6d.gph, ///
    ycommon cols(2) ///
    saving(Figure_9.gph, replace) ///
    xsize(10) ysize(13)

// Save the Graph in the Figures Folder
graph export "$Figures/Figure 6.pdf" , replace

********************************************************************************
* Figure 7: Long-Term Effects on Trust in Parliament (Panels A–B)
********************************************************************************

// -----------------------------------------------------------------------------
// Panel A: Government Transition
// -----------------------------------------------------------------------------
use "$Data/Results+meta.dta", clear

// Recode week variables to avoid negative values in factor variable interactions
replace week = week + 100

// Estimate model for cases of government transition
reg trstprl i.week##i.winner eduyrs agea gndr lrscale polintr hincfel ///
    i.country i.essround if transition == 1, robust

// Marginal effects for winner across weeks
margins, dydx(winner) at(week == (98(1)105))

// Plot
marginsplot, ///
    xline(100) ///
    xtitle("Week") ///
    ytitle("Trust in Parliament") ///
    title("Government Transition") ///
    xlabel(98 "-2" 99 "-1" 100 "0" 101 "1" 102 "2" 103 "3" 104 "4" 105 "5") ///
    ylabel(-1 "-1" 0 "0" 1 "1" 2 "2" 3 "3") ///
    yline(0) ///
    yscale(range(-1.5 1.5))

graph save Figure_7a.gph, replace

// -----------------------------------------------------------------------------
// Panel B: Incumbent Re-election
// -----------------------------------------------------------------------------

// Estimate model for cases of incumbent reelection
reg trstprl i.week##i.winner eduyrs agea gndr lrscale polintr hincfel ///
    i.country i.essround if transition == 0, robust

// Marginal effects
margins, dydx(winner) at(week == (98(1)105))

// Plot
marginsplot, ///
    xline(100) ///
    xtitle("Week") ///
    ytitle("") ///
    title("Incumbent Re-election") ///
    xlabel(98 "-2" 99 "-1" 100 "0" 101 "1" 102 "2" 103 "3" 104 "4" 105 "5") ///
    ylabel(-1 "-1" 0 "0" 1 "1" 2 "2" 3 "3") ///
    yline(0) ///
    yscale(range(-1.5 1.5))

graph save Figure_7b.gph, replace

// -----------------------------------------------------------------------------
// Combine Panels A and B
// -----------------------------------------------------------------------------
graph combine Figure_7a.gph Figure_7b.gph, ///
    saving(Figure_7, replace) ///
    ycommon xcommon

// Save the Graph in the Figures Folder
graph export "$Figures/Figure 7.pdf" , replace

********************************************************************************
* Figure B1: Power Tests Losers
********************************************************************************

// Plot power tests for different bandwidths
rdpow stfdem days if winner==0, covs(country* essround) p(1) h(100 100) kernel(epanechnikov) graph_options(title("100±") xtitle(Effect Size) ytitle("Power") yline(1) xline(1.026)) plot
graph save Figure_0_100_power.gph, replace

rdpow stfdem days if winner==0, covs(country* essround) p(1) h(30 30) kernel(epanechnikov) graph_options(title("30±") xtitle(Effect Size) ytitle("Power") yline(0.992) xline(1.026)) plot
graph save Figure_0_30_power.gph, replace

rdpow stfdem days if winner==0, covs(country* essround) p(1) h(20 20) kernel(epanechnikov) graph_options(title("20±") xtitle(Effect Size) ytitle("Power") yline(0.931) xline(1.014)) plot
graph save Figure_0_20_power.gph, replace

rdpow stfdem days if winner==0, covs(country* essround) p(1) h(16 16) kernel(epanechnikov) graph_options(title("16±") xtitle(Effect Size) ytitle("Power") yline(0.833) xline(1.009)) plot
graph save Figure_0_16_power.gph, replace

// Combine all panels into one figure
graph combine ///
    Figure_0_100_power.gph ///
    Figure_0_30_power.gph ///
    Figure_0_20_power.gph ///
    Figure_0_16_power.gph, ///
    saving(Figure_power_tests_losers_combined.gph, replace) ///
    title("Power Tests: Losers") ///
    rows(2) ysize(12) xsize(10)

// Save the Graph in the Figures Folder
graph export "$Figures/Figure B1.pdf" , replace

********************************************************************************
* Figure B2: Power Tests Winners
********************************************************************************

// Plot power tests for different bandwidths
rdpow stfdem days if winner==1, covs(country* essround) p(1) h(100 100) kernel(epanechnikov) graph_options(title("100±") xtitle(Effect Size) ytitle("Power") yline(1) xline(1.011)) plot
graph save Figure_1_100_power.gph, replace

rdpow stfdem days if winner==1, covs(country* essround) p(1) h(30 30) kernel(epanechnikov) graph_options(title("30±") xtitle(Effect Size) ytitle("Power") yline(0.993) xline(1.031)) plot
graph save Figure_1_30_power.gph, replace

rdpow stfdem days if winner==1, covs(country* essround) p(1) h(20 20) kernel(epanechnikov) graph_options(title("20±") xtitle(Effect Size) ytitle("Power") yline(0.941) xline(1.023)) plot
graph save Figure_1_20_power.gph, replace

rdpow stfdem days if winner==1, covs(country* essround) p(1) h(16 16) kernel(epanechnikov) graph_options(title("16±") xtitle(Effect Size) ytitle("Power") yline(0.842) xline(1)) plot
graph save Figure_1_16_power.gph, replace

graph combine Figure_1_100_power.gph Figure_1_30_power.gph Figure_1_20_power.gph Figure_1_16_power.gph, saving(Figure_power_tests_winners_combined.gph, replace)   title("Power Tests: Winners") rows(2) ysize(12) xsize(10)

// Combine all panels into one figure
graph combine ///
    Figure_1_100_power.gph ///
    Figure_1_30_power.gph ///
    Figure_1_20_power.gph ///
    Figure_1_16_power.gph, ///
    saving(Figure_power_tests_winners_combined.gph, replace) ///
    title("Power Tests: Winners") ///
    rows(2) ysize(12) xsize(10)
	
// Save the Graph in the Figures Folder
graph export "$Figures/Figure B2.pdf" , replace

********************************************************************************
* Figure B3. Placebo test: Politically interested winners
********************************************************************************

// Loop over cutpoints from -10 to 10 and store estimates
forvalues c = -10/10 {
    local label = cond(`c' < 0, "N_`=abs(`c')'", cond(`c' == 0, "N_0", "P_`c'"))
    rdrobust stfdem days if winner==1 & polintr<3, covs(country* inwyys) c(`c') p(1) masspoints(off) kernel(epanechnikov)
    estimates store `label'
}

// Generate the coefplot
coefplot (N_10, aseq(-10) ///
        \ N_9, aseq(-9) ///
		\ N_8, aseq(-8) ///
		\ N_7, aseq(-7) ///
		\ N_6, aseq(-6) ///
		\ N_5, aseq(-5) ///
		\ N_4, aseq(-4) ///
		\ N_3, aseq(-3) ///
		\ N_2, aseq(-2) ///
		\ N_1, aseq(-1) ///
		\ N_0, aseq(0) ///
		\ P_1, aseq(1) ///
		\ P_2, aseq(2) ///
		\ P_3, aseq(3) ///
		\ P_4, aseq(4) ///
		\ P_5, aseq(5) ///
		\ P_6, aseq(6) ///
		\ P_7, aseq(7) ///
		\ P_8, aseq(8) ///
		\ P_9, aseq(9) ///
		\ P_10, aseq(10)) ///
       , drop(_cons) swapnames xline(0) yscale(lstyle(none))  title("Placebo Tests: Politically Interested Winners") 

// Save the Graph in the Figures Folder
graph export "$Figures/Figure B3.pdf" , replace

********************************************************************************
* Figure B4. The Long-Term Effect – Extended Time Frame
********************************************************************************

use "$Data/Results+meta.dta", clear

// Recode week variables to avoid negative values in factor variable interactions
replace week = week + 100

// -----------------------------------------------------------------------------
* Panel A: Government Transition
// -----------------------------------------------------------------------------

reg stfdem i.week##i.winner eduyrs agea gndr lrscale polintr hincfel ///
    i.country i.essround if transition == 1, robust

margins, dydx(winner) at(week == (90(1)110))

marginsplot, ///
    xline(100) ///
    xtitle("Week") ///
    ytitle("Satisfaction with Democracy") ///
    title("Government Transition") ///
    xlabel(90 "-10" 95 "-5" 100 "0" 105 "5" 110 "10") ///
    ylabel(-2(1)3) ///
    yline(0) ///
    yscale(range(-1 3))

graph save Figure_B4a.gph, replace

// -----------------------------------------------------------------------------
* Panel B: Incumbent Re-election
// -----------------------------------------------------------------------------

reg stfdem i.week##i.winner eduyrs agea gndr lrscale polintr hincfel ///
    i.country i.essround if transition == 0, robust

margins, dydx(winner) at(week == (90(1)110))

marginsplot, ///
    xline(100) ///
    xtitle("Week") ///
    ytitle("") ///
    title("Incumbent Re-election") ///
    xlabel(90 "-10" 95 "-5" 100 "0" 105 "5" 110 "10") ///
    ylabel(-2(1)3) ///
    yline(0) ///
    yscale(range(-1 3))

graph save Figure_B4b.gph, replace

// -----------------------------------------------------------------------------
* Combine Panel A and B
// -----------------------------------------------------------------------------

graph combine Figure_B4a.gph Figure_B4b.gph, ///
    ycommon xcommon ///
    saving(Figure_B4.gph, replace)

// Save the Graph in the Figures Folder
graph export "$Figures/Figure B4.pdf" , replace

********************************************************************************
* Figure C.1. McCrary Test – Density of Running Variable for Winners and Losers
********************************************************************************

// Generate the density plot
rddensity days if !missing(winner), plot

// Modify the graph (e.g., make it grayscale)
gr_edit .plotregion1.plot1.style.editstyle area(linestyle(color(gs1))) editcopy

gr_edit .plotregion1.plot2.style.editstyle area(linestyle(color(gs1))) editcopy

gr_edit .plotregion1.plot1.style.editstyle area(shadestyle(color(gs1))) editcopy

gr_edit .plotregion1.plot2.style.editstyle area(shadestyle(color(gs1))) editcopy

gr_edit .plotregion1.plot3.style.editstyle area(shadestyle(color(gs1))) editcopy

gr_edit .plotregion1.plot4.style.editstyle area(shadestyle(color(gs1))) editcopy

gr_edit .plotregion1.plot5.style.editstyle line(color(gs6))

gr_edit .plotregion1.plot6.style.editstyle line(color(gs6))

// Save the Graph in the Figures Folder
graph export "$Figures/Figure C1.pdf" , replace

********************************************************************************
* Table 1. Comparing the Winner–Loser Gap:
* Government Transitions vs. Incumbent Reelections
* Across Post-Election Periods
********************************************************************************

*---------------------------------------------------------------
* Period: Day 0–30
*---------------------------------------------------------------
use "$Data/Results+meta.dta", clear

// Restrict sample
drop if days < 0 | days > 30

// Incumbent Reelections – No Controls
reg stfdem winner i.country i.essround if transition == 0, robust
outreg2 treatment using "$Tables/Table_1", excel replace keep(winner) label addnote("Country and survey round fixed effects included in all models.")

// Incumbent Reelections – With Controls
reg stfdem winner i.country i.essround agea gndr eduyrs lrscale polintr hincfel ///
    if transition == 0, robust
outreg2 treatment using "$Tables/Table_1", excel append keep(winner agea gndr eduyrs lrscale polintr hincfel) label

// Government Transitions – No Controls
reg stfdem winner i.country i.essround if transition == 1, robust
outreg2 treatment using "$Tables/Table_1", excel append keep(winner) label

// Government Transitions – With Controls
reg stfdem winner i.country i.essround agea gndr eduyrs lrscale polintr hincfel ///
    if transition == 1, robust
outreg2 treatment using "$Tables/Table_1", excel append keep(winner agea gndr eduyrs lrscale polintr hincfel) label

*---------------------------------------------------------------
* Period: Day 30–150
*---------------------------------------------------------------
use "$Data/Results+meta.dta", clear

// Restrict sample
drop if days < 30 | days > 150

// Incumbent Reelections – No Controls
reg stfdem winner i.country i.essround if transition == 0, robust
outreg2 treatment using "$Tables/Table_1", excel append keep(winner) label

// Incumbent Reelections – With Controls
reg stfdem winner i.country i.essround agea gndr eduyrs lrscale polintr hincfel ///
    if transition == 0, robust
outreg2 treatment using "$Tables/Table_1", excel append keep(winner agea gndr eduyrs lrscale polintr hincfel) label

// Government Transitions – No Controls
reg stfdem winner i.country i.essround if transition == 1, robust
outreg2 treatment using "$Tables/Table_1", excel append keep(winner) label

// Government Transitions – With Controls
reg stfdem winner i.country i.essround agea gndr eduyrs lrscale polintr hincfel ///
    if transition == 1, robust
outreg2 treatment using "$Tables/Table_1", excel append keep(winner agea gndr eduyrs lrscale polintr hincfel) label

********************************************************************************
* Table B1. The Winner–Loser Gap Within the Sample
********************************************************************************

*---------------------------------------------------------------
* Table B1. The Winner–Loser Gap Within the Sample
*---------------------------------------------------------------
use "$Data/Results+meta.dta", clear

// Without controls
reg stfdem current_winner i.country i.essround, robust
outreg2 treatment using "$Tables/Table_B1", excel replace keep(current_winner) label addnote("Country and survey round fixed effects included in all models.")

// With controls
reg stfdem current_winner i.country i.essround ///
    agea gndr eduyrs stfeco lrscale polintr nwspol hincfel, robust
outreg2 treatment using "$Tables/Table_B1", excel append keep(current_winner agea gndr eduyrs stfeco lrscale polintr nwspol hincfel) label

********************************************************************************
* Table B2. RD Estimates – Losers
********************************************************************************

use "$Data/Results+meta.dta", clear

// RD with FE and lag controls, 100-day bandwidth
rdrobust stfdem days if winner == 0, ///
    covs(country* essround lag_days_stfdem) ///
    masspoints(off) p(1) kernel(epanechnikov) h(100 100)
	
// Store the effective sample size
local Neff = e(N_h_l) + e(N_h_r)

// Export with effective sample size
outreg2 treatment using "$Tables/Table_B2", excel replace addstat("Observations (effective)", `Neff') noobs label

// RD with alternative bandwidths (30, 20, 16), FE only
rdrobust stfdem days if winner == 0, ///
    covs(country* essround) h(30 30) p(1) ///
    masspoints(off) kernel(epanechnikov)
local Neff = e(N_h_l) + e(N_h_r)
outreg2 treatment using "$Tables/Table_B2", excel append addstat("Observations (effective)", `Neff') noobs label

rdrobust stfdem days if winner == 0, ///
    covs(country* essround) h(20 20) p(1) ///
    masspoints(off) kernel(epanechnikov)
local Neff = e(N_h_l) + e(N_h_r)
outreg2 treatment using "$Tables/Table_B2", excel append addstat("Observations (effective)", `Neff') noobs label

rdrobust stfdem days if winner == 0, ///
    covs(country* essround) h(16 16) p(1) ///
    masspoints(off) kernel(epanechnikov)
local Neff = e(N_h_l) + e(N_h_r)
outreg2 treatment using "$Tables/Table_B2", excel append addstat("Observations (effective)", `Neff') noobs label

********************************************************************************
* Table B3. RD Estimates – Winners
********************************************************************************

use "$Data/Results+meta.dta", clear

// Full sample of winners, FE and lag controls, 100-day bandwidth
rdrobust stfdem days if winner == 1, ///
    covs(country* essround lag_days_stfdem) ///
    p(1) kernel(epanechnikov) masspoints(off) h(100 100)
	
// Store the effective sample size
local Neff = e(N_h_l) + e(N_h_r)

// Export with effective sample size
outreg2 treatment using "$Tables/Table_B3", excel replace addstat("Observations (effective)", `Neff') noobs label

// Politically interested winners (polintr < 3), FE and lag controls, 100-day bandwidth
rdrobust stfdem days if winner == 1 & polintr < 3, ///
    covs(country* essround lag_days_stfdem) ///
    p(1) kernel(epanechnikov) masspoints(off) h(100 100)
local Neff = e(N_h_l) + e(N_h_r)
outreg2 treatment using "$Tables/Table_B3", excel append addstat("Observations (effective)", `Neff') noobs label

// Politically interested winners (polintr < 3), FE and lag controls, 30-day bandwidth
rdrobust stfdem days if winner == 1 & polintr < 3, ///
    covs(country* essround lag_days_stfdem) ///
    p(1) kernel(epanechnikov) masspoints(off) h(30 30)
local Neff = e(N_h_l) + e(N_h_r)
outreg2 treatment using "$Tables/Table_B3", excel append addstat("Observations (effective)", `Neff') noobs label

// Politically interested winners (polintr < 3), FE and lag controls, 20-day bandwidth
rdrobust stfdem days if winner == 1 & polintr < 3, ///
    covs(country* essround lag_days_stfdem) ///
    p(1) kernel(epanechnikov) masspoints(off) h(20 20)
local Neff = e(N_h_l) + e(N_h_r)
outreg2 treatment using "$Tables/Table_B3", excel append addstat("Observations (effective)", `Neff') noobs label

// Politically interested winners (polintr < 3), FE and lag controls, 16-day bandwidth
rdrobust stfdem days if winner == 1 & polintr < 3, ///
    covs(country* essround lag_days_stfdem) ///
    p(1) kernel(epanechnikov) masspoints(off) h(16 16)
local Neff = e(N_h_l) + e(N_h_r)
outreg2 treatment using "$Tables/Table_B3", excel append addstat("Observations (effective)", `Neff') noobs label

********************************************************************************
* Table B4. RD Estimates – Winners with Full Controls and Clustering
********************************************************************************

use "$Data/Results+meta.dta", clear

// Full sample of winners, 100-day bandwidth, clustered SEs
rdrobust stfdem days if winner==1, covs(country* essround agea gndr eduyrs lrscale hincfel total_visits total_refusals lag_days_stfdem)  p(1) kernel(epanechnikov) h(100 100) vce(cluster cluster_id)

// Store the effective sample size
local Neff = e(N_h_l) + e(N_h_r)

// Export with effective sample size
outreg2 treatment using "$Tables/Table_B4", excel replace addstat("Observations (effective)", `Neff') noobs label

// Politically interested winners (polintr < 3), 100-day bandwidth, clustered SEs
rdrobust stfdem days if winner==1 & polintr<3, covs(country* essround agea gndr eduyrs lrscale hincfel total_visits total_refusals lag_days_stfdem) p(1) kernel(epanechnikov) h(100 100) vce(cluster cluster_id)
local Neff = e(N_h_l) + e(N_h_r)
outreg2 treatment using "$Tables/Table_B4", excel append addstat("Observations (effective)", `Neff') noobs label

// Politically interested winners (polintr < 3), 30-day bandwidth, clustered SEs
rdrobust stfdem days if winner==1 & polintr<3, covs(country* essround agea gndr eduyrs lrscale hincfel total_visits total_refusals lag_days_stfdem) p(1) kernel(epanechnikov) h(30 30) vce(cluster cluster_id)
local Neff = e(N_h_l) + e(N_h_r)
outreg2 treatment using "$Tables/Table_B4", excel append addstat("Observations (effective)", `Neff') noobs label

// Politically interested winners (polintr < 3), 20-day bandwidth, clustered SEs
rdrobust stfdem days if winner==1 & polintr<3, covs(country* essround agea gndr eduyrs lrscale hincfel total_visits total_refusals lag_days_stfdem) p(1) kernel(epanechnikov) h(20 20) vce(cluster cluster_id)
local Neff = e(N_h_l) + e(N_h_r)
outreg2 treatment using "$Tables/Table_B4", excel append addstat("Observations (effective)", `Neff') noobs label

// Politically interested winners (polintr < 3), 16-day bandwidth, clustered SEs
rdrobust stfdem days if winner==1 & polintr<3, covs(country* essround agea gndr eduyrs lrscale hincfel total_visits total_refusals lag_days_stfdem) p(1) kernel(epanechnikov) h(16 16) vce(cluster cluster_id)
local Neff = e(N_h_l) + e(N_h_r)
outreg2 treatment using "$Tables/Table_B4", excel append addstat("Observations (effective)", `Neff') noobs label

********************************************************************************
* Table B5. Comparing the Winner–Loser Gap:
* Government Transitions vs. Incumbent Reelections
* Across Post-Election Periods
********************************************************************************

*---------------------------------------------------------------
* Period: Day 0–30
*---------------------------------------------------------------
use "$Data/Results+meta.dta", clear

// Restrict sample
drop if days < 0 | days > 30

// Incumbent Reelections – No Controls
reg stfdem winner i.country i.essround if transition == 0, robust
outreg2 treatment using "$Tables/Table_B5", excel replace keep(winner) label addnote("Country and survey round fixed effects included in all models.")

// Incumbent Reelections – With Controls
reg stfdem winner i.country i.essround agea gndr eduyrs lrscale polintr hincfel ///
    if transition == 0, robust
outreg2 treatment using "$Tables/Table_B5", excel append keep(winner agea gndr eduyrs lrscale polintr hincfel)  label

// Government Transitions – No Controls
reg stfdem winner i.country i.essround if transition == 1, robust
outreg2 treatment using "$Tables/Table_B5", excel append keep(winner) label

// Government Transitions – With Controls
reg stfdem winner i.country i.essround agea gndr eduyrs lrscale polintr hincfel ///
    if transition == 1, robust
outreg2 treatment using "$Tables/Table_B5", excel append keep(winner agea gndr eduyrs lrscale polintr hincfel) label

*---------------------------------------------------------------
* Period: Day 30–150
*---------------------------------------------------------------
use "$Data/Results+meta.dta", clear

// Restrict sample
drop if days < 30 | days > 150

// Incumbent Reelections – No Controls
reg stfdem winner i.country i.essround if transition == 0, robust
outreg2 treatment using "$Tables/Table_B5", excel append keep(winner) label

// Incumbent Reelections – With Controls
reg stfdem winner i.country i.essround agea gndr eduyrs lrscale polintr hincfel ///
    if transition == 0, robust
outreg2 treatment using "$Tables/Table_B5", excel append keep(winner agea gndr eduyrs lrscale polintr hincfel) label

// Government Transitions – No Controls
reg stfdem winner i.country i.essround if transition == 1, robust
outreg2 treatment using "$Tables/Table_B5", excel append keep(winner) label

// Government Transitions – With Controls
reg stfdem winner i.country i.essround agea gndr eduyrs lrscale polintr hincfel ///
    if transition == 1, robust
outreg2 treatment using "$Tables/Table_B5", excel append keep(winner agea gndr eduyrs lrscale polintr hincfel) label

********************************************************************************
* Table B6. Long-Term Effects Among Radical and Moderate Voters
********************************************************************************

use "$Data/Results+meta.dta", clear

// Create a dummy variable: 1 = ideologically extreme (left < 4 or right > 6), 0 = moderate (4–6) 
generate extreme = .
replace extreme = 1 if (lrscale < 4 | lrscale > 6)
replace extreme = 0 if lrscale > 3 & lrscale < 7

label variable extreme "Radical"

// Run regression to test interaction effects between treatment, time, and ideological extremity
reg stfdem c.week##i.winner##i.extreme i.country i.inwyys if days > -1, robust

// Export results
outreg2 treatment using "$Tables/Table_B6", excel replace keep(week winner 1.extreme  extreme#c.week winner#extreme winner#extreme#c.week) label addnote("Country and survey round fixed effects included.")

********************************************************************************
* Table B7. Winner-Loser Gap by Political Knowledge
********************************************************************************

use "$Data/Results+meta.dta", clear

// Create index of news consumption from TV and newspaper consumption
gen news = .
replace news = (tvpol + nwsptot) / 2 if tvpol != . & nwsptot != .

// Create dummy: 1 = high political knowledge, 0 = low political knowledge
gen news_consumption = .
replace news_consumption = 1 if news > 3
replace news_consumption = 0 if news < 4

label variable news_consumption "News Consumption"

// Regression to test interaction between time, winner status, and political knowledge
reg stfdem c.week##i.winner##i.news_consumption i.country i.essround i.transition##c.week if days > -1, robust

// Export results 
outreg2 treatment using "$Tables/Table_B7", excel replace keep(week winner winner#c.week 1.news_consumption news_consumption#c.week winner#news_consumption winner#news_consumption#c.week) label addnote("Country and survey round fixed effects included.")

********************************************************************************
* Table C1. Political Trust
********************************************************************************

use "$Data/Results+meta.dta", clear

// RDrobust for trust in public officials (tstpboh) among losers
rdrobust tstpboh days if winner == 0, covs(country* essround) p(1) masspoints(off) kernel(epanechnikov)
outreg2 treatment using "$Tables/Table_C1", excel replace label

// RDrobust for trust in police (trstplc) among losers
rdrobust trstplc days if winner == 0, covs(country* essround) p(1) masspoints(off) kernel(epanechnikov)
outreg2 treatment using "$Tables/Table_C1", excel append label

// RDrobust for trust in political parties (trstprt) among losers
rdrobust trstprt days if winner == 0, covs(country* essround) p(1) masspoints(off) kernel(epanechnikov)
outreg2 treatment using "$Tables/Table_C1", excel append label

// RDrobust for trust in public officials (tstpboh) among winners
rdrobust tstpboh days if winner == 1, covs(country* essround) p(1) masspoints(off) kernel(epanechnikov)
outreg2 treatment using "$Tables/Table_C1", excel append label

// RDrobust for trust in police (trstplc) among winners
rdrobust trstplc days if winner == 1, covs(country* essround) p(1) masspoints(off) kernel(epanechnikov)
outreg2 treatment using "$Tables/Table_C1", excel append label

// RDrobust for trust in political parties (trstprt) among winners
rdrobust trstprt days if winner == 1, covs(country* essround) p(1) masspoints(off) kernel(epanechnikov)
outreg2 treatment using "$Tables/Table_C1", excel append label

********************************************************************************
* Table C2. Number of winners/losers in the sample before and after the cut-off point
********************************************************************************

use "$Data/Results+meta.dta", clear

// RDrobust for winners/losers with bandwidth 100
rdrobust winner days, covs(country* essround) p(1) masspoints(off) h(100 100) kernel(epanechnikov)

// Store the effective sample size
local Neff = e(N_h_l) + e(N_h_r)

// Export with effective sample size
outreg2 treatment using "$Tables/Table_C2", excel replace addstat("Observations (effective)", `Neff') noobs label

// RDrobust for winners/losers with bandwidth 30
rdrobust winner days, covs(country* essround) p(1) masspoints(off) h(30 30) kernel(epanechnikov)
local Neff = e(N_h_l) + e(N_h_r)
outreg2 treatment using "$Tables/Table_C2", excel append addstat("Observations (effective)", `Neff') noobs label

// RDrobust for winners/losers with bandwidth 20
rdrobust winner days, covs(country* essround) p(1) masspoints(off) h(20 20) kernel(epanechnikov)
local Neff = e(N_h_l) + e(N_h_r)
outreg2 treatment using "$Tables/Table_C2", excel append addstat("Observations (effective)", `Neff') noobs label

// RDrobust for winners/losers with bandwidth 16
rdrobust winner days, covs(country* essround) p(1) masspoints(off) h(16 16) kernel(epanechnikov)
local Neff = e(N_h_l) + e(N_h_r)
outreg2 treatment using "$Tables/Table_C2", excel append addstat("Observations (effective)", `Neff') noobs label

********************************************************************************
* Table C3. Election day as cut-off and fuzzy cut-off
********************************************************************************

// Upload dataset 
use "$Data/Results+meta.dta", clear

// Restrict sample to center around election day
drop if days_election < -100
drop if days_election > 100

// RDrobust for winners (using election day as the cut-off)
rdrobust stfdem days_election if winner==0, covs(country* essround) masspoints(off) p(1) kernel(epanechnikov) 
outreg2 treatment using "$Tables/Table_C3", excel replace label

// RDrobust for winners (using election day as the cut-off)
rdrobust stfdem days_election if winner==1, covs(country* essround) masspoints(off) p(1) kernel(epanechnikov) 
outreg2 treatment using "$Tables/Table_C3", excel append label

// RDrobust for losers (fuzzy cut-off)
rdrobust stfdem days if winner==0, covs(country* essround) masspoints(off) p(1) kernel(epanechnikov) fuzzy(after)
outreg2 treatment using "$Tables/Table_C3", excel append label

// RDrobust for winners (fuzzy cut-off)
rdrobust stfdem days if winner==1, covs(country* essround) masspoints(off) p(1) kernel(epanechnikov) fuzzy(after)
outreg2 treatment using "$Tables/Table_C3", excel append label

********************************************************************************
* Table C4. RD Estimates Using Higher-Order Polynomials
********************************************************************************

// Upload dataset 
use "$Data/Results+meta.dta", clear

// RD for losers: quadratic polynomial (p = 2)
rdrobust stfdem days if winner==0, covs(country* inwyys) p(2) kernel(epanechnikov) masspoints(off) h(100 100)   

// Store the effective sample size
local Neff = e(N_h_l) + e(N_h_r)

// Export with effective sample size
outreg2 treatment using "$Tables/Table_C4", excel replace addstat("Observations (effective)", `Neff') noobs label

// RD for losers: cubic polynomial (p = 3)
rdrobust stfdem days if winner==0, covs(country* inwyys) p(3) kernel(epanechnikov) masspoints(off) h(100 100)   
local Neff = e(N_h_l) + e(N_h_r)
outreg2 treatment using "$Tables/Table_C4", excel append addstat("Observations (effective)", `Neff') noobs label

// RD for winners: quadratic polynomial (p = 2)
rdrobust stfdem days if winner==1, covs(country* inwyys) p(2) kernel(epanechnikov) masspoints(off) h(100 100)   
local Neff = e(N_h_l) + e(N_h_r)
outreg2 treatment using "$Tables/Table_C4", excel append addstat("Observations (effective)", `Neff') noobs label

// RD for winners: cubic polynomial (p = 3)
rdrobust stfdem days if winner==1, covs(country* inwyys) p(3) kernel(epanechnikov) masspoints(off) h(100 100)   
local Neff = e(N_h_l) + e(N_h_r)
outreg2 treatment using "$Tables/Table_C4", excel append addstat("Observations (effective)", `Neff') noobs label

********************************************************************************
* Table C5. RD Estimates with Squared Outcome Variable
********************************************************************************

// RD for losers using squared outcome variable
rdrobust stfdem_squared days if winner==0, covs(country* inwyys) p(1) kernel(epanechnikov) masspoints(off) h(100 100) 

// Store the effective sample size
local Neff = e(N_h_l) + e(N_h_r)

// Export with effective sample size
outreg2 treatment using "$Tables/Table_C5", excel replace addstat("Effective N", `Neff') noobs label

// RD for winners using squared outcome variable
rdrobust stfdem_squared days if winner==1, covs(country* inwyys) p(1) masspoints(off) kernel(epanechnikov) h(100 100)   
local Neff = e(N_h_l) + e(N_h_r)
outreg2 treatment using "$Tables/Table_C5", excel append addstat("Effective N", `Neff') noobs label

********************************************************************************
* Table C6. Descriptive Statistics
********************************************************************************

// Load data and generate descriptive statistics for key variables
use "$Data/Results+meta.dta", clear

// Generate and export summary statistics for key variables
asdoc sum after agea ctzcntr days eduyrs gndr hincfel hswrk lrscale rdpol nwsptot pdwrk polintr rtrd stfdem stfeco edctn trstprl trstprt trstplc tstpboh uempla tvpol winner,  save(Tables/Table_C6.doc) replace label

********************************************************************************
* End of do-file
********************************************************************************
